Skip to content

Commit

Permalink
Update grid calls & run tests on develop (MHKiT-Software#226)
Browse files Browse the repository at this point in the history
* Update calls to grid formatting

* run on develop
  • Loading branch information
ssolson authored and jmcvey3 committed Apr 14, 2023
1 parent 5713f9e commit 16aed26
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 36 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ on:
push:
branches:
- master
- Develop
- develop
pull_request:
branches:
- master
- Develop
- develop

jobs:
conda-build:
Expand Down
30 changes: 19 additions & 11 deletions mhkit/river/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,18 @@ def _xy_plot(x, y, fmt='.', label=None, xlabel=None, ylabel=None, title=None,

ax.plot(x, y, fmt, label=label, markersize=7)

ax.grid(b=True, which='both')
ax.grid()

if label:
ax.legend()
if xlabel:
ax.set_xlabel(xlabel)
if ylabel:
ax.set_ylabel(ylabel)
if title:
ax.set_title(title)
if label: ax.legend()
if xlabel: ax.set_xlabel(xlabel)
if ylabel: ax.set_ylabel(ylabel)
if title: ax.set_title(title)

plt.tight_layout()

return ax


def plot_flow_duration_curve(D, F, label=None, ax=None):
"""
Plots discharge vs exceedance probability as a Flow Duration Curve (FDC)
Expand Down Expand Up @@ -81,6 +78,7 @@ def plot_flow_duration_curve(D, F, label=None, ax=None):

return ax


def plot_velocity_duration_curve(V, F, label=None, ax=None):
"""
Plots velocity vs exceedance probability as a Velocity Duration Curve (VDC)
Expand Down Expand Up @@ -114,6 +112,7 @@ def plot_velocity_duration_curve(V, F, label=None, ax=None):

return ax


def plot_power_duration_curve(P, F, label=None, ax=None):
"""
Plots power vs exceedance probability as a Power Duration Curve (PDC)
Expand Down Expand Up @@ -147,6 +146,7 @@ def plot_power_duration_curve(P, F, label=None, ax=None):

return ax


def plot_discharge_timeseries(Q, label=None, ax=None):
"""
Plots discharge time-series
Expand All @@ -168,11 +168,19 @@ def plot_discharge_timeseries(Q, label=None, ax=None):
ax : matplotlib pyplot axes
"""
ax = _xy_plot(Q.index, Q, fmt='-', label=label, xlabel='Time',
ylabel='Discharge [$m^3/s$]', ax=ax)
ax = _xy_plot(
Q.index,
Q,
fmt='-',
label=label,
xlabel='Time',
ylabel='Discharge [$m^3/s$]',
ax=ax
)

return ax


def plot_discharge_vs_velocity(D, V, polynomial_coeff=None, label=None, ax=None):
"""
Plots discharge vs velocity data along with the polynomial fit
Expand Down
4 changes: 3 additions & 1 deletion mhkit/river/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def exceedance_probability(D):
F = F.to_frame('F') # for matlab

return F



def polynomial_fit(x, y, n):
"""
Returns a polynomial fit for y given x of order n
Expand Down Expand Up @@ -180,6 +181,7 @@ def velocity_to_power(V, polynomial_coefficients, cut_in, cut_out):

return P


def energy_produced(P, seconds):
"""
Returns the energy produced for a given time period provided
Expand Down
12 changes: 10 additions & 2 deletions mhkit/tests/river/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ def setUpClass(self):
def tearDownClass(self):
pass


def test_Froude_number(self):
v = 2
h = 5
Fr = river.resource.Froude_number(v, h)
self.assertAlmostEqual(Fr, 0.286, places=3)


def test_exceedance_probability(self):
# Create arbitrary discharge between 0 and 8(N=9)
Q = pd.Series(np.arange(9))
Expand All @@ -49,6 +51,7 @@ def test_exceedance_probability(self):
self.assertEqual(f.min().values , 10. )
self.assertEqual(f.max().values , 90. )


def test_polynomial_fit(self):
# Calculate a first order polynomial on an x=y line
p, r2 = river.resource.polynomial_fit(np.arange(8), np.arange(8),1)
Expand All @@ -69,6 +72,7 @@ def test_discharge_to_velocity(self):
V = river.resource.discharge_to_velocity(Q, p)
self.assertAlmostEqual(np.sum(10*Q - V['V']), 0.00, places=2 )


def test_velocity_to_power(self):
# Calculate a first order polynomial on an DV_Curve x=y line 10 times greater than the Q values
p, r2 = river.resource.polynomial_fit(np.arange(9), 10*np.arange(9),1)
Expand Down Expand Up @@ -118,6 +122,7 @@ def test_plot_flow_duration_curve(self):

self.assertTrue(isfile(filename))


def test_plot_power_duration_curve(self):
filename = abspath(join(plotdir, 'river_plot_power_duration_curve.png'))
if isfile(filename):
Expand All @@ -131,6 +136,7 @@ def test_plot_power_duration_curve(self):

self.assertTrue(isfile(filename))


def test_plot_velocity_duration_curve(self):
filename = abspath(join(plotdir, 'river_plot_velocity_duration_curve.png'))
if isfile(filename):
Expand All @@ -144,10 +150,10 @@ def test_plot_velocity_duration_curve(self):

self.assertTrue(isfile(filename))


def test_plot_discharge_timeseries(self):
filename = abspath(join(plotdir, 'river_plot_discharge_timeseries.png'))
if isfile(filename):
os.remove(filename)
if isfile(filename): os.remove(filename)

plt.figure()
river.graphics.plot_discharge_timeseries(self.data['Q'])
Expand All @@ -156,6 +162,7 @@ def test_plot_discharge_timeseries(self):

self.assertTrue(isfile(filename))


def test_plot_discharge_vs_velocity(self):
filename = abspath(join(plotdir, 'river_plot_discharge_vs_velocity.png'))
if isfile(filename):
Expand All @@ -168,6 +175,7 @@ def test_plot_discharge_vs_velocity(self):

self.assertTrue(isfile(filename))


def test_plot_velocity_vs_power(self):
filename = abspath(join(plotdir, 'river_plot_velocity_vs_power.png'))
if isfile(filename):
Expand Down
57 changes: 41 additions & 16 deletions mhkit/tidal/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,15 @@ def _initialize_polar(ax=None, metadata=None, flood=None, ebb=None):
return ax


def plot_rose(directions, velocities, width_dir, width_vel, metadata=None,
flood=None, ebb=None):
def plot_rose(
directions,
velocities,
width_dir,
width_vel,
metadata=None,
flood=None,
ebb=None
):
"""
Creates a polar histogram. Direction angles from binned histogram must
be specified such that 0 degrees is north.
Expand Down Expand Up @@ -173,9 +180,15 @@ def plot_rose(directions, velocities, width_dir, width_vel, metadata=None,
return ax


def plot_joint_probability_distribution(directions, velocities, width_dir,
width_vel, metadata=None,
flood=None, ebb=None):
def plot_joint_probability_distribution(
directions,
velocities,
width_dir,
width_vel,
metadata=None,
flood=None,
ebb=None
):
"""
Creates a polar histogram. Direction angles from binned histogram must
be specified such that 0 is north.
Expand Down Expand Up @@ -315,8 +328,15 @@ def plot_current_timeseries(directions, velocities, principal_direction,
xlabel='Time', ylabel='Velocity [$m/s$]', ax=ax)
return ax

def tidal_phase_probability(directions, velocities, flood, ebb,
bin_size=0.1, ax=None):

def tidal_phase_probability(
directions,
velocities,
flood,
ebb,
bin_size=0.1,
ax=None
):
'''
Discretizes the tidal series speed by bin size and returns a plot
of the probability for each bin in the flood or ebb tidal phase.
Expand Down Expand Up @@ -353,17 +373,17 @@ def tidal_phase_probability(directions, velocities, flood, ebb,
assert isinstance(ebb, (int, float)), \
'ebb must be of type int or float'
assert isinstance(bin_size, (int, float)), \
'bin_size must be of type int or float'
'bin_size must be of type int or float'
assert flood >=0 and flood <=360,\
'flood must be between 0 and 360 degrees'
assert ebb >=0 and ebb <=360,\
'ebb must be between 0 and 360 degrees'
'ebb must be between 0 and 360 degrees'
assert bin_size >=0 ,\
'bin_size must be greater than 0'
'bin_size must be greater than 0'

if ax==None:
fig, ax = plt.subplots(figsize=(12, 8))

isEbb = _flood_or_ebb(directions, flood, ebb)

decimals = round(bin_size/0.1)
Expand Down Expand Up @@ -394,13 +414,18 @@ def tidal_phase_probability(directions, velocities, flood, ebb,
plt.ylim(0,1.0)
plt.legend()
plt.grid(linestyle=':')

return ax



def tidal_phase_exceedance(directions, velocities, flood, ebb,
bin_size=0.1, ax=None):
def tidal_phase_exceedance(
directions,
velocities,
flood,
ebb,
bin_size=0.1,
ax=None
):
'''
Returns a stacked area plot of the exceedance probability for the
flood and ebb tidal phases.
Expand Down
8 changes: 4 additions & 4 deletions mhkit/wave/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ def plot_compendium(Hs, Tp, Dp, buoy_title=None, ax=None):
pHs.set_ylim(0,8)
pHs.tick_params(axis='y', which='major', labelsize=12, right='off')
pHs.set_ylabel('Hs [m]', fontsize=18)
pHs.grid(b=True, which='major', color='b', linestyle='--')
pHs.grid(color='b', linestyle='--')

pHs2 = pHs.twinx()
pHs2.set_ylim(0,25)
Expand All @@ -611,13 +611,13 @@ def plot_compendium(Hs, Tp, Dp, buoy_title=None, ax=None):
# Peak Period, Tp
pTp.set_ylim(0,28)
pTp.set_ylabel('Tp [s]', fontsize=18)
pTp.grid(b=True, which='major', color='b', linestyle='--')
pTp.grid(color='b', linestyle='--')


# Direction, Dp
pDp.set_ylim(0,360)
pDp.set_ylabel('Dp [deg]', fontsize=18)
pDp.grid(b=True, which='major', color='b', linestyle='--')
pDp.grid(color='b', linestyle='--')
pDp.set_xlabel('Day', fontsize=18)

# Set x-axis tick interval to every 5 days
Expand Down Expand Up @@ -736,7 +736,7 @@ def plot_boxplot(Hs, buoy_title=None):
bp.tick_params(axis='x', which='major', labelsize=12, top='off')

# Plot horizontal gridlines onto top subplot
bp.grid(axis='x', which='major', color='b', linestyle='-', alpha=0.25)
bp.grid(axis='x', color='b', linestyle='-', alpha=0.25)

# Remove tickmarks from bottom subplot
bp2.axes.get_xaxis().set_visible(False)
Expand Down

0 comments on commit 16aed26

Please sign in to comment.