Skip to content

Commit

Permalink
edits for pre-commit fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cyschneck committed Mar 19, 2024
1 parent e7a0fde commit b7df9cc
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions centerline_width/river_asymmetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
Rivers, 2024, Nature Communication
'''

def find_inflection_pt_index(x_points,y_points):

def find_inflection_pt_index(x_points, y_points):
'''
find where the slope of the curvature
changes sign from negative to positive
ie half of the inflection points
'''

# interpolate to get continious function
# interpolate to get continuous function
lin_interp = interp1d(x_points, y_points, kind='linear')

x_interp = np.linspace(x_points[0], x_points[-1], len(x_points))
Expand All @@ -27,6 +28,7 @@ def find_inflection_pt_index(x_points,y_points):

return inflection_pt_indx


def xy_coord(centerline_coordinates: np.ndarray) -> np.ndarray:
'''
return path as x,y pairs
Expand All @@ -40,9 +42,10 @@ def xy_coord(centerline_coordinates: np.ndarray) -> np.ndarray:
x_coordinates.append(centerline_point[0])
y_coordinates.append(centerline_point[1])

return np.array(x_coordinates),np.array(y_coordinates)
return np.array(x_coordinates), np.array(y_coordinates)


def find_curvature(x: np.ndarray,y:np.ndarray)-> np.ndarray:
def find_curvature(x: np.ndarray, y: np.ndarray) -> np.ndarray:
'''
find curvature at each point on path
'''
Expand All @@ -51,11 +54,14 @@ def find_curvature(x: np.ndarray,y:np.ndarray)-> np.ndarray:
dy = np.gradient(y)
ddx = np.gradient(dx)
ddy = np.gradient(dy)
curvature_path = np.abs(dx * ddy - dy * ddx) / (dx ** 2 + dy ** 2) ** 1.5
curvature_path = np.abs(dx * ddy - dy * ddx) / (dx**2 + dy**2)**1.5

return curvature_path

def smooth_curvature(curvature_path: np.ndarray, poly_order: int = 3, window_size: int = 21) -> np.ndarray:

def smooth_curvature(curvature_path: np.ndarray,
poly_order: int = 3,
window_size: int = 21) -> np.ndarray:
'''
smooth curvature using low-pass filter (Savitzky–Golay)
'''
Expand All @@ -64,7 +70,9 @@ def smooth_curvature(curvature_path: np.ndarray, poly_order: int = 3, window_siz

return smoothed_curvature

def find_apex_index(smoothed_curvature: np.ndarray, inflection_pt_index: np.ndarray) -> np.ndarray:

def find_apex_index(smoothed_curvature: np.ndarray,
inflection_pt_index: np.ndarray) -> np.ndarray:
'''
find river apex indices aka points of max curvature
between successive inflection points
Expand All @@ -75,35 +83,35 @@ def find_apex_index(smoothed_curvature: np.ndarray, inflection_pt_index: np.ndar
start_index = inflection_pt_index[i]
end_index = inflection_pt_index[i + 1]
if start_index != end_index:
max_index = start_index + np.argmax(smoothed_curvature[start_index:end_index])
max_index = start_index + np.argmax(
smoothed_curvature[start_index:end_index])
apex_index.append(max_index)

return np.array(apex_index)


if __name__ == '__main__':

# TESTING ON A RANDOM RIVER
num_pts = 1000
scl = 0.01
t = np.linspace(0, 10*np.pi, num_pts)
t = np.linspace(0, 10 * np.pi, num_pts)
x = t
y = np.sin(t) + np.random.normal(scale=scl, size=num_pts)

fig, axs = plt.subplots(2)
axs[0].plot(x,y,'k.-')

axs[0].plot(x, y, 'k.-')

curv = find_curvature(x,y)
curv = find_curvature(x, y)
scurv = smooth_curvature(curv)
ip_idx = find_inflection_pt_index(t,scurv)
apex = find_apex_index(scurv,ip_idx)
ip_idx = find_inflection_pt_index(t, scurv)
apex = find_apex_index(scurv, ip_idx)
#axs[1].plot(t,curv,'k.-', label = 'curvature')
axs[1].plot(t,scurv,'k.-', label = 'smoothed curvature')
axs[1].plot(t, scurv, 'k.-', label='smoothed curvature')

for ind in ip_idx:
axs[1].axvline(x=t[ind], color='r', linestyle='--', alpha=0.5)


plt.scatter(t[apex], scurv[apex], color='r', label='Apex')

axs[0].set_title('River')
Expand All @@ -114,4 +122,3 @@ def find_apex_index(smoothed_curvature: np.ndarray, inflection_pt_index: np.ndar
axs[1].set_ylabel('curvature')
plt.legend()
plt.show()

0 comments on commit b7df9cc

Please sign in to comment.