TomasBeuzen / pybeach

A Python package for locating the dune toe on cross-shore beach profile transects.
MIT License
18 stars 12 forks source link

How to extract elevation of toes? #17

Closed ontherocks123 closed 1 year ago

ontherocks123 commented 1 year ago

Hello! Great program, been using it with my own terrestrial LiDAR data and its working great. I am trying to plot the elevations of many dune toes and how they vary over years of data and cannot seem to isolate just the elevation value of the toe_ml variable, it is only outputting the value of the spot along the line where the toe_ml is located, not an elevation. I am aware this is just a coding issue, not specific to pybeach but any help would be greatly appreciated! Just looking to isolate the z value(elevation) of toe_ml .

I am using a csv file, not .pkl, which might be adding to the issue... Here's my code:

input_data_file = "D:/Output tables/2018_LiDAR.csv" profile = pd.read_csv(input_data_file)

x = np.arange(len(profile))

x = profile['FIRST_DIST'].to_numpy() z = profile['FIRST_Z'].to_numpy() p = Profile(x, z)

plt.plot(x, z, '-k')

instantiate

p = Profile(x, z)

predict dune toe, dune crest, shoreline location

toe_ml, prob_ml = p.predict_dunetoe_ml('wave_embayed_clf') # predict toe using machine learning model toe_mc = p.predict_dunetoe_mc() # predict toe using maximum curvature method (Stockdon et al, 2007) toe_rr = p.predict_dunetoe_rr() # predict toe using relative relief method (Wernette et al, 2016) toe_pd = p.predict_dunetoe_pd() # predict toe using perpendicular distance method crest = p.predict_dunecrest() # predict dune crest shoreline = p.predict_shoreline() # predict shoreline

fig, axes = plt.subplots(1, 1, figsize=(7, 5)) toe = [toe_ml, toe_mc, toe_rr, toe_pd] labels = ['True toe', 'Machine learning toe', 'Maximum curvature toe', 'Relative relief toe', 'Perpendicular distance'] colors = ['lime', 'tomato', 'cornflowerblue', 'gold', 'limegreen'] axes.plot(x, z, '-k') axes.fill_between([70, 100], [0, 0], y2=-1, color='lightskyblue', alpha=0.5) axes.fill_between(x, z, y2=-1, color='cornsilk', alpha=1) axes.axvspan(-10, -9, color='tomato', alpha = 0.6, label='ML Toe probability') # legend placeholder

for i, itoe in enumerate(toe): axes.plot(x[itoe], z [itoe], 'o', color=colors[i], ms=12, mec='k', label=labels[i])

axes.plot(x[crest], z[crest], 'v', color='k', ms=12, mec='k', label='Crest') axes.plot(x[shoreline], z[shoreline], '^', color='k', ms=12, mec='k', label='Shoreline')

axes.set_xlim(200, 400)

axes.set_ylim(0, 6)

plt.set_title('Example profile')

axes.set_xlabel('Cross-shore distance (ft)') axes.set_ylabel('Elevation (ft)') axes.grid() axes.legend(loc='lower left')

df = pd.DataFrame({'MAE': [np.absolute(toe-toe_ml).mean(), np.absolute(toe-toe_mc).mean(), np.absolute(toe-toe_rr).mean(), np.absolute(toe-toe_pd).mean()], 'RMSE': [np.sqrt(np.square(toe-toe_ml).mean()), np.sqrt(np.square(toe-toe_mc).mean()), np.sqrt(np.square(toe-toe_rr).mean()), np.sqrt(np.square(toe-toe_pd).mean())]},

              index=['ML', 'MC', 'RR', 'PD']).round(2)

print(toe_ml)

[43]

chrisleaman commented 1 year ago

toe_ml returns indicies of the dune toe locations in your x and z arrays. So building on the example in the readme:

# example data
x = np.arange(0, 80, 0.5)
z = np.concatenate((np.linspace(4, 5, 40),
                    np.linspace(5, 2, 10),
                    np.linspace(2, 0, 91)[1:],
                    np.linspace(0, -1, 20)))

# instantiate
p = Profile(x, z)

# predict dune toe, dune crest, shoreline location
toe_ml, prob_ml = p.predict_dunetoe_ml('wave_embayed_clf')  # predict toe using machine learning model

# toe_ml can contain multiple indicies, so just take first value as the most
# probable
x_toe = x[toe_ml][0]
z_toe = z[toe_ml][0]
print(f'Dune toe located at x={x_toe}m, z={z_toe}m ')

# plot
fig, ax = plt.subplots(1,1)
ax.plot(x,z,)
ax.scatter(x_toe, z_toe)

will output:

Dune toe located at x=24.5m, z=2.0m 

H3mtODW6hY

Hope this helps!

ontherocks123 commented 1 year ago

Thanks so much!

TomasBeuzen commented 1 year ago

Thanks @chrisleaman! 🙏🎉