Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lint river module #389

Draft
wants to merge 39 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
bc1eadc
Use `main` as Primary Branch (#367)
ssolson Dec 9, 2024
c27bc3a
lint tidal io
ssolson Mar 10, 2025
d08d4c3
Merge branch 'develop' of https://github.com/MHKiT-Software/MHKiT-Pyt…
ssolson Mar 10, 2025
3a4751c
linted graphics
ssolson Mar 11, 2025
ca64da2
lint performance
ssolson Mar 12, 2025
63f8d74
lint resource
ssolson Mar 12, 2025
b11392e
pylint tidal
ssolson Mar 12, 2025
fb9fce0
fix examples
ssolson Mar 12, 2025
1da136e
noaa typehints
ssolson Mar 18, 2025
e349897
resource type hints
ssolson Mar 18, 2025
16cd953
graphics type hints
ssolson Mar 18, 2025
b7dc99e
performance type hints
ssolson Mar 18, 2025
8abb811
pylint performance.py
ssolson Mar 18, 2025
d5808f6
add doc strings where missing
ssolson Mar 18, 2025
8156135
pylint: fix line too long
ssolson Mar 18, 2025
d7b532e
consistent module docstrings
ssolson Mar 18, 2025
1ea8c5d
Merge branch 'develop' of https://github.com/MHKiT-Software/MHKiT-Pyt…
ssolson Mar 31, 2025
cfbad56
pylint
ssolson Apr 1, 2025
757a533
pylint
ssolson Apr 1, 2025
92e3d98
pylint river io
ssolson Apr 1, 2025
a8c6972
pylint
ssolson Apr 1, 2025
dd358d5
pylint
ssolson Apr 1, 2025
e06d95d
pylint
ssolson Apr 1, 2025
a91dfb8
pylint river
ssolson Apr 1, 2025
9f574d9
typehints
ssolson Apr 1, 2025
5f4d6b2
froude_number
ssolson Apr 1, 2025
7428eb1
pylint
ssolson Apr 1, 2025
a28d333
Merge branch 'develop' of https://github.com/MHKiT-Software/MHKiT-Pyt…
ssolson Apr 9, 2025
709c4ba
simplify module doc string
ssolson Apr 9, 2025
45c9ff6
froude_number
ssolson Apr 9, 2025
ed3911d
simplify the docstrings
ssolson Apr 9, 2025
44b9846
black
ssolson Apr 9, 2025
9eb296d
module docstring
ssolson Apr 9, 2025
971689e
adjust for variable name changes
ssolson Apr 10, 2025
a366c28
adjsut for name changes
ssolson Apr 10, 2025
f48ccbf
adjust for new api
ssolson Apr 10, 2025
6579d5d
pylint
ssolson Apr 10, 2025
8670235
fix api changes
ssolson Apr 10, 2025
761df55
exceedance_probability
ssolson Apr 11, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ jobs:
run: |
pylint mhkit/acoustics/

- name: Run Pylint on mhkit/tidal
- name: Run Pylint on mhkit/tidal/
run: |
pylint mhkit/tidal/

- name: Run Pylint on mhkit/river/
run: |
pylint --extension-pkg-allow-list=netCDF4 mhkit/river/
215 changes: 105 additions & 110 deletions examples/ADCP_Delft3D_TRTS_example.ipynb

Large diffs are not rendered by default.

57 changes: 28 additions & 29 deletions examples/river_example.ipynb

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions mhkit/river/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""
The river module provides tools and utilities for analyzing river energy resources.

"""

from mhkit.river import performance
from mhkit.river import graphics
from mhkit.river import resource
Expand Down
143 changes: 100 additions & 43 deletions mhkit/river/graphics.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
"""
graphics.py

The graphics module provides plotting utilities for river energy resource data.

"""

from typing import Union, Optional
import numpy as np
import xarray as xr
import matplotlib.pyplot as plt
from matplotlib.axes import Axes
from numpy.typing import ArrayLike
from mhkit.utils import convert_to_dataarray


def _xy_plot(x, y, fmt=".", label=None, xlabel=None, ylabel=None, title=None, ax=None):
# pylint: disable=too-many-arguments
# pylint: disable=too-many-positional-arguments
def _xy_plot(
x: ArrayLike,
y: ArrayLike,
fmt: str = ".",
label: Optional[str] = None,
xlabel: Optional[str] = None,
ylabel: Optional[str] = None,
title: Optional[str] = None,
ax: Optional[Axes] = None,
) -> Axes:
"""
Base function to plot any x vs y data

Expand Down Expand Up @@ -50,16 +71,21 @@ def _xy_plot(x, y, fmt=".", label=None, xlabel=None, ylabel=None, title=None, ax
return ax


def plot_flow_duration_curve(D, F, label=None, ax=None):
def plot_flow_duration_curve(
discharge: Union[ArrayLike, xr.DataArray],
exceedance_prob: Union[ArrayLike, xr.DataArray],
label: Optional[str] = None,
ax: Optional[Axes] = None,
) -> Axes:
"""
Plots discharge vs exceedance probability as a Flow Duration Curve (FDC)

Parameters
------------
D: array-like
discharge: array-like
Discharge [m/s] indexed by time

F: array-like
exceedance_prob: array-like
Exceedance probability [unitless] indexed by time

label: string
Expand All @@ -74,13 +100,15 @@ def plot_flow_duration_curve(D, F, label=None, ax=None):
ax : matplotlib pyplot axes

"""
# Sort by F
temp = xr.Dataset(data_vars={"D": D, "F": F})
temp = temp.sortby("F", ascending=False)
# Sort by exceedance_prob
temp = xr.Dataset(
data_vars={"discharge": discharge, "exceedance_prob": exceedance_prob}
)
temp = temp.sortby("exceedance_prob", ascending=False)

ax = _xy_plot(
temp["D"],
temp["F"],
temp["discharge"],
temp["exceedance_prob"],
fmt="-",
label=label,
xlabel="Discharge [$m^3/s$]",
Expand All @@ -92,16 +120,21 @@ def plot_flow_duration_curve(D, F, label=None, ax=None):
return ax


def plot_velocity_duration_curve(V, F, label=None, ax=None):
def plot_velocity_duration_curve(
velocity: Union[ArrayLike, xr.DataArray],
exceedance_prob: Union[ArrayLike, xr.DataArray],
label: Optional[str] = None,
ax: Optional[Axes] = None,
) -> Axes:
"""
Plots velocity vs exceedance probability as a Velocity Duration Curve (VDC)

Parameters
------------
V: array-like
velocity: array-like
Velocity [m/s] indexed by time

F: array-like
exceedance_prob: array-like
Exceedance probability [unitless] indexed by time

label: string
Expand All @@ -116,13 +149,15 @@ def plot_velocity_duration_curve(V, F, label=None, ax=None):
ax : matplotlib pyplot axes

"""
# Sort by F
temp = xr.Dataset(data_vars={"V": V, "F": F})
temp = temp.sortby("F", ascending=False)
# Sort by exceedance_prob
temp = xr.Dataset(
data_vars={"velocity": velocity, "exceedance_prob": exceedance_prob}
)
temp = temp.sortby("exceedance_prob", ascending=False)

ax = _xy_plot(
temp["V"],
temp["F"],
temp["velocity"],
temp["exceedance_prob"],
fmt="-",
label=label,
xlabel="Velocity [$m/s$]",
Expand All @@ -133,16 +168,21 @@ def plot_velocity_duration_curve(V, F, label=None, ax=None):
return ax


def plot_power_duration_curve(P, F, label=None, ax=None):
def plot_power_duration_curve(
power: Union[ArrayLike, xr.DataArray],
exceedance_prob: Union[ArrayLike, xr.DataArray],
label: Optional[str] = None,
ax: Optional[Axes] = None,
) -> Axes:
"""
Plots power vs exceedance probability as a Power Duration Curve (PDC)

Parameters
------------
P: array-like
power: array-like
Power [W] indexed by time

F: array-like
exceedance_prob: array-like
Exceedance probability [unitless] indexed by time

label: string
Expand All @@ -157,13 +197,13 @@ def plot_power_duration_curve(P, F, label=None, ax=None):
ax : matplotlib pyplot axes

"""
# Sort by F
temp = xr.Dataset(data_vars={"P": P, "F": F})
temp.sortby("F", ascending=False)
# Sort by exceedance_prob
temp = xr.Dataset(data_vars={"power": power, "exceedance_prob": exceedance_prob})
temp.sortby("exceedance_prob", ascending=False)

ax = _xy_plot(
temp["P"],
temp["F"],
temp["power"],
temp["exceedance_prob"],
fmt="-",
label=label,
xlabel="Power [W]",
Expand All @@ -174,13 +214,18 @@ def plot_power_duration_curve(P, F, label=None, ax=None):
return ax


def plot_discharge_timeseries(Q, time_dimension="", label=None, ax=None):
def plot_discharge_timeseries(
discharge: Union[ArrayLike, xr.DataArray],
time_dimension: str = "",
label: Optional[str] = None,
ax: Optional[Axes] = None,
) -> Axes:
"""
Plots discharge time-series

Parameters
------------
Q: array-like
discharge: array-like
Discharge [m3/s] indexed by time

time_dimension: string (optional)
Expand All @@ -199,14 +244,14 @@ def plot_discharge_timeseries(Q, time_dimension="", label=None, ax=None):
ax : matplotlib pyplot axes

"""
Q = convert_to_dataarray(Q)
discharge = convert_to_dataarray(discharge)

if time_dimension == "":
time_dimension = list(Q.coords)[0]
time_dimension = list(discharge.coords)[0]

ax = _xy_plot(
Q.coords[time_dimension].values,
Q,
discharge.coords[time_dimension].values,
discharge,
fmt="-",
label=label,
xlabel="Time",
Expand All @@ -217,16 +262,22 @@ def plot_discharge_timeseries(Q, time_dimension="", label=None, ax=None):
return ax


def plot_discharge_vs_velocity(D, V, polynomial_coeff=None, label=None, ax=None):
def plot_discharge_vs_velocity(
discharge: Union[ArrayLike, xr.DataArray],
velocity: Union[ArrayLike, xr.DataArray],
polynomial_coeff: Optional[np.poly1d] = None,
label: Optional[str] = None,
ax: Optional[Axes] = None,
) -> Axes:
"""
Plots discharge vs velocity data along with the polynomial fit

Parameters
------------
D : array-like
discharge : array-like
Discharge [m/s] indexed by time

V : array-like
velocity : array-like
Velocity [m/s] indexed by time

polynomial_coeff: numpy polynomial
Expand All @@ -244,16 +295,16 @@ def plot_discharge_vs_velocity(D, V, polynomial_coeff=None, label=None, ax=None)

"""
ax = _xy_plot(
D,
V,
discharge,
velocity,
fmt=".",
label=label,
xlabel="Discharge [$m^3/s$]",
ylabel="Velocity [$m/s$]",
ax=ax,
)
if polynomial_coeff:
x = np.linspace(D.min(), D.max())
x = np.linspace(discharge.min(), discharge.max())
ax = _xy_plot(
x,
polynomial_coeff(x),
Expand All @@ -267,16 +318,22 @@ def plot_discharge_vs_velocity(D, V, polynomial_coeff=None, label=None, ax=None)
return ax


def plot_velocity_vs_power(V, P, polynomial_coeff=None, label=None, ax=None):
def plot_velocity_vs_power(
velocity: Union[ArrayLike, xr.DataArray],
power: Union[ArrayLike, xr.DataArray],
polynomial_coeff: Optional[np.poly1d] = None,
label: Optional[str] = None,
ax: Optional[Axes] = None,
) -> Axes:
"""
Plots velocity vs power data along with the polynomial fit

Parameters
------------
V : array-like
velocity : array-like
Velocity [m/s] indexed by time

P: array-like
power: array-like
Power [W] indexed by time

polynomial_coeff: numpy polynomial
Expand All @@ -294,16 +351,16 @@ def plot_velocity_vs_power(V, P, polynomial_coeff=None, label=None, ax=None):

"""
ax = _xy_plot(
V,
P,
velocity,
power,
fmt=".",
label=label,
xlabel="Velocity [$m/s$]",
ylabel="Power [$W$]",
ax=ax,
)
if polynomial_coeff:
x = np.linspace(V.min(), V.max())
x = np.linspace(velocity.min(), velocity.max())
ax = _xy_plot(
x,
polynomial_coeff(x),
Expand Down
7 changes: 7 additions & 0 deletions mhkit/river/io/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
"""
river.io

This module provides input/output functionality for river energy related data in MHKiT.

"""

from mhkit.river.io import usgs
from mhkit.river.io import d3d
Loading
Loading