Skip to content

Commit

Permalink
performance update
Browse files Browse the repository at this point in the history
  • Loading branch information
changliao1025 committed Feb 28, 2024
1 parent c4c8bf5 commit 8d2de4a
Show file tree
Hide file tree
Showing 16 changed files with 420 additions and 357 deletions.
45 changes: 27 additions & 18 deletions pyflowline/algorithms/auxiliary/check_head_water.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,30 @@ def check_head_water(aFlowline_in, pVertex_start_in):
Returns:
[int]: [0: not headwater; 1: is headwater]
"""
nFlowline = len(aFlowline_in)
iFlag_head_water = -1
iCount = 0
for i in range(nFlowline):
pFlowline = aFlowline_in[i]
pVerter_start = pFlowline.pVertex_start
pVerter_end = pFlowline.pVertex_end
if pVerter_start == pVertex_start_in:
iCount = iCount + 1
pass
if pVerter_end == pVertex_start_in:
iCount = iCount + 1
pass
pass
if iCount == 1:
iFlag_head_water=1

return iFlag_head_water
#nFlowline = len(aFlowline_in)
#iFlag_head_water = -1
#iCount = 0
#for i in range(nFlowline):
# pFlowline = aFlowline_in[i]
# pVerter_start = pFlowline.pVertex_start
# pVerter_end = pFlowline.pVertex_end
# if pVerter_start == pVertex_start_in:
# iCount = iCount + 1
# pass
# if pVerter_end == pVertex_start_in:
# iCount = iCount + 1
# pass
# pass
#if iCount == 1:
# iFlag_head_water=1
#
#return iFlag_head_water
#Create sets of all start and end vertices
start_vertices = {flowline.pVertex_start for flowline in aFlowline_in}
end_vertices = {flowline.pVertex_end for flowline in aFlowline_in}

# Check if the vertex is a headwater
is_headwater = pVertex_start_in in start_vertices and pVertex_start_in not in end_vertices

return int(is_headwater)

2 changes: 0 additions & 2 deletions pyflowline/algorithms/auxiliary/find_vertex_in_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ def find_vertex_in_list(aVertex_in, pVertex_in, dThreshold_in=1.0E-6):
iFlag_exist = 0
lIndex= -1
nVertex= len(aVertex_in)

if iFlag_use_rtree == 1:

#can we use rtree here?
#index_vertex = rtree.index.Index()
index_vertex = RTree(max_cap=5, min_cap=2)
Expand Down
23 changes: 13 additions & 10 deletions pyflowline/algorithms/direction/correct_flowline_direction.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,29 @@ def correct_flowline_direction(aFlowline_in, pVertex_outlet_in):
aFlag_process=None
global lFlowlineIndex
nFlowline = len(aFlowline_in)
# Create sets for faster lookup
pVertex_start_in_set = {flowline.pVertex_start for flowline in aFlowline_in}
pVertex_end_in_set = {flowline.pVertex_end for flowline in aFlowline_in}

aFlag_process=np.full(nFlowline, 0, dtype =int)
iFlag_first = 1
for i in range(nFlowline):
pFlowline = aFlowline_in[i]
iFlag_dam = pFlowline.iFlag_dam
iStream_order = pFlowline.iStream_order
pVertex_start = pFlowline.pVertex_start
pVertex_end = pFlowline.pVertex_end
dDiatance = pVertex_end.calculate_distance( pVertex_outlet_in )
pVertex_end = pFlowline.pVertex_end
if iFlag_first == 1:
dDiatance_min = dDiatance
dDiatance_min = pVertex_end.calculate_distance( pVertex_outlet_in )
lIndex_outlet = i
iFlag_first=0
else:
else:
dDiatance = pFlowline.pVertex_end.calculate_distance(pVertex_outlet_in)
if dDiatance < dDiatance_min:
dDiatance_min = dDiatance
#found it
lIndex_outlet = i
pass
else:

pass


lFlowlineIndex = 0
pFlowline = aFlowline_in[lIndex_outlet]
Expand All @@ -59,15 +60,17 @@ def find_upstream_flowline(pVertex_start_in, pVertex_end_in):
pFlowline = aFlowline_in[i]
pVerter_start = pFlowline.pVertex_start
pVerter_end = pFlowline.pVertex_end
if pVerter_end == pVertex_start_in and pVerter_start!=pVertex_end_in:
#if pVerter_end == pVertex_start_in and pVerter_start!=pVertex_end_in:
if pVerter_end in pVertex_start_in_set and pVerter_start != pVertex_end_in:
if aFlag_process[i] != 1:
nUpstream = nUpstream + 1
aUpstream.append(i)
aReverse.append(0)
aFlag_process[i] = 1
pass
else:
if pVerter_start == pVertex_start_in and pVerter_end !=pVertex_end_in :
#if pVerter_start == pVertex_start_in and pVerter_end !=pVertex_end_in :
if pVerter_start in pVertex_start_in_set and pVerter_end != pVertex_end_in:
if aFlag_process[i] != 1:
nUpstream = nUpstream + 1
aUpstream.append(i)
Expand Down
22 changes: 19 additions & 3 deletions pyflowline/algorithms/loop/remove_flowline_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ def remove_flowline_loop(aFlowline_in):
"""
aFlowline_out=list()
nFlowline = len(aFlowline_in)

# Create a dictionary that maps pVertex_start to a list of flowlines
flowline_dict = {}
for i in range(nFlowline):
pFlowline = aFlowline_in[i]
pVertex_start = pFlowline.pVertex_start
if pVertex_start not in flowline_dict:
flowline_dict[pVertex_start] = []
flowline_dict[pVertex_start].append((i, pFlowline))

def find_paralle_stream( pVertex_start_in):
ndownstream=0
aDownstream=list()
Expand All @@ -32,7 +42,14 @@ def find_paralle_stream( pVertex_start_in):
pVertex_start = pFlowline.pVertex_start
pVertex_end = pFlowline.pVertex_end
iStream_order = pFlowline.iStream_order
ndownstream , aDownstream, aStream_order = find_paralle_stream( pVertex_start)
#ndownstream , aDownstream, aStream_order = find_paralle_stream( pVertex_start)

# Get all parallel streams
parallel_streams = flowline_dict.get(pVertex_start, [])
ndownstream = len(parallel_streams)
aDownstream = [j for j, _ in parallel_streams]
aStream_order = [flowline.iStream_order for _, flowline in parallel_streams]

if ndownstream == 1:
if aFlag[i] !=1:
pFlowline.lFlowlineIndex = lID
Expand All @@ -56,7 +73,6 @@ def find_paralle_stream( pVertex_start_in):

for k in range(ndownstream):
aFlag[ aDownstream[k] ] = 1
pass
pass


return aFlowline_out
24 changes: 15 additions & 9 deletions pyflowline/algorithms/merge/merge_flowline.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

import numpy as np


import importlib
iFlag_cython = importlib.util.find_spec("cython")
if iFlag_cython is not None:
Expand Down Expand Up @@ -43,9 +41,15 @@ def merge_flowline(aFlowline_in,

aVertex_headwater=aVertex[aIndex_headwater]
aVertex_middle=aVertex[aIndex_middle]

#convert to set
aVertex_headwater_set = set(aVertex[aIndex_headwater])
aVertex_middle_set = set(aVertex[aIndex_middle])

if aIndex_confluence.size > 0:
iFlag_confluence = 1
aVertex_confluence=aVertex[aIndex_confluence]
aVertex_confluence_set = set(aVertex[aIndex_confluence])
else:
iFlag_confluence = 0
pass
Expand All @@ -56,7 +60,8 @@ def merge_flowline_reach(lIndex_in, pVertex_start_in, pVertex_end_in):
iSegment = pFlowline.iStream_segment
pVertex_current = pVertex_start_in

while (find_vertex_in_list(aVertex_middle.tolist(), pVertex_current)[0] ==1):
#while (find_vertex_in_list(aVertex_middle.tolist(), pVertex_current)[0] ==1):
while (pVertex_current in aVertex_middle_set):
for j in range(0, nFlowline):
pFlowline2 = aFlowline_in[j]
pVertex_start = pFlowline2.pVertex_start
Expand All @@ -65,12 +70,11 @@ def merge_flowline_reach(lIndex_in, pVertex_start_in, pVertex_end_in):
pFlowline = pFlowline.merge_upstream(pFlowline2)
pVertex_current = pVertex_start
break
else:
pass



#go to next
if find_vertex_in_list(aVertex_headwater.tolist(), pVertex_current)[0] ==1:
#if find_vertex_in_list(aVertex_headwater.tolist(), pVertex_current)[0] ==1:
if pVertex_current in aVertex_headwater_set:
pFlowline.iStream_segment = iSegment
pFlowline.iStream_order = 1
pFlowline.lFlowlineIndex = lID
Expand All @@ -83,7 +87,8 @@ def merge_flowline_reach(lIndex_in, pVertex_start_in, pVertex_end_in):
aFlowline_out.append(pFlowline)
lID = lID + 1
#confluence
if find_vertex_in_list(aVertex_confluence.tolist(), pVertex_current)[0] ==1:
#if find_vertex_in_list(aVertex_confluence.tolist(), pVertex_current)[0] ==1:
if pVertex_current in aVertex_confluence_set:
for k in range(0, nFlowline):
pFlowline3 = aFlowline_in[k]
pVertex_start = pFlowline3.pVertex_start
Expand Down Expand Up @@ -118,7 +123,8 @@ def merge_flowline_reach(lIndex_in, pVertex_start_in, pVertex_end_in):
#now start from outlet
if iFlag_confluence == 1:
#check whether outlet is a confluence
if (find_vertex_in_list(aVertex_confluence.tolist(), pVertex_end)[0] ==1):
#if (find_vertex_in_list(aVertex_confluence.tolist(), pVertex_end)[0] ==1):
if pVertex_end in aVertex_confluence_set:
for i in range(nFlowline):
pFlowline = aFlowline_in[i]
pVertex_start_dummy = pFlowline.pVertex_start
Expand Down
53 changes: 25 additions & 28 deletions pyflowline/algorithms/simplification/remove_small_river.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,35 @@ def remove_small_river(aFlowline_in, dThreshold_in):
Returns:
List: Theortically, the flowline should be ordered from outlet to headwater
"""
nFlowline = len(aFlowline_in)
aFlowline_out=list()
if nFlowline == 1:
aFlowline_out.append(aFlowline_in[0])
"""

if len(aFlowline_in) == 1:
return [aFlowline_in[0]]
else:
aFlowline_out=list()
lID = 0
for i in range(nFlowline):
pFlowline = aFlowline_in[i]
iFlag_dam = pFlowline.iFlag_dam
pVertex_start = pFlowline.pVertex_start
pVertex_end = pFlowline.pVertex_end
for pFlowline in aFlowline_in:
iFlag_dam = pFlowline.iFlag_dam
dLength = pFlowline.calculate_length()
if iFlag_dam ==1:
if iFlag_dam == 1 or pFlowline.iStream_order != 1 or dLength > dThreshold_in:
pFlowline.lFlowlineIndex = lID
aFlowline_out.append(pFlowline)
lID = lID +1
else:
if pFlowline.iStream_order == 1:
if dLength > dThreshold_in :
pFlowline.lFlowlineIndex = lID
aFlowline_out.append(pFlowline)
lID = lID + 1
pass
else:
pass
pass
else: #this one might be not used
pFlowline.lFlowlineIndex = lID
aFlowline_out.append(pFlowline)
lID = lID +1
pass
lID += 1
#if iFlag_dam ==1:
# pFlowline.lFlowlineIndex = lID
# aFlowline_out.append(pFlowline)
# lID = lID +1
#else:
# if pFlowline.iStream_order == 1:
# if dLength > dThreshold_in :
# pFlowline.lFlowlineIndex = lID
# aFlowline_out.append(pFlowline)
# lID = lID + 1
# else: #this one might be not used
# pFlowline.lFlowlineIndex = lID
# aFlowline_out.append(pFlowline)
# lID = lID +1
# pass
#if check_head_water(aFlowline_in, pVertex_start)==1:
# if dLength > dThreshold_in :
# pFlowline.lIndex = lID
Expand All @@ -54,6 +51,6 @@ def remove_small_river(aFlowline_in, dThreshold_in):
# lID = lID +1
# pass

pass


return aFlowline_out
Loading

0 comments on commit 8d2de4a

Please sign in to comment.