Skip to content

Commit

Permalink
Maintenance: Upgrade Outdated Python Style (NREL#274)
Browse files Browse the repository at this point in the history
* run pyupgrade --py38-plus against codebase

* add pyupgrade to pre-commit workflow

* fix bad fix
  • Loading branch information
RHammond2 authored Feb 16, 2024
1 parent 1d4074e commit c0485c8
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 54 deletions.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ repos:
additional_dependencies: [toml]
types: [pyi]

- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
hooks:
- id: pyupgrade
args: [--py38-plus]

- repo: https://github.com/psf/black
rev: 23.3.0
Expand Down
24 changes: 9 additions & 15 deletions openoa/analysis/aep.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def __attrs_post_init__(self):
else:
analysis_type = "MonteCarloAEP"

if set((analysis_type, "all")).intersection(self.plant.analysis_type) == set():
if {analysis_type, "all"}.intersection(self.plant.analysis_type) == set():
self.plant.analysis_type.append(analysis_type)

# Ensure the data are up to spec before continuing with initialization
Expand Down Expand Up @@ -377,7 +377,7 @@ def run(
num_sim=self.num_sim,
reanalysis_products=self.reanalysis_products,
)
logger.info("Running with parameters: {}".format(logged_params))
logger.info(f"Running with parameters: {logged_params}")

# Start the computation
self.calculate_long_term_losses()
Expand Down Expand Up @@ -572,11 +572,9 @@ def process_reanalysis_data(self):

if self.end_date_lt > end_date:
raise ValueError(
(
"Invalid end date for long-term correction. The end date cannot exceed the "
"last full time period (defined by the time resolution) in the provided "
"reanalysis data."
)
"Invalid end date for long-term correction. The end date cannot exceed the "
"last full time period (defined by the time resolution) in the provided "
"reanalysis data."
)
else:
# replace end date
Expand All @@ -601,17 +599,13 @@ def process_reanalysis_data(self):
if self._reanalysis_aggregate.index[0] > start_date_required:
if self.end_date_lt is not None:
raise ValueError(
(
"Invalid end date argument for long-term correction. This end date does not "
"provide enough reanalysis data for the long-term correction."
)
"Invalid end date argument for long-term correction. This end date does not "
"provide enough reanalysis data for the long-term correction."
)
else:
raise ValueError(
(
"The date range of the provided reanalysis data is not long enough to "
"perform the long-term correction."
)
"The date range of the provided reanalysis data is not long enough to "
"perform the long-term correction."
)

# Correct each reanalysis product, density-correct wind speeds, and take monthly averages
Expand Down
4 changes: 2 additions & 2 deletions openoa/analysis/electrical_losses.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def __attrs_post_init__(self):
"""
Initialize logging and post-initialization setup steps.
"""
if set(("ElectricalLosses", "all")).intersection(self.plant.analysis_type) == set():
if {"ElectricalLosses", "all"}.intersection(self.plant.analysis_type) == set():
self.plant.analysis_type.append("ElectricalLosses")

# Ensure the data are up to spec before continuing with initialization
Expand Down Expand Up @@ -371,7 +371,7 @@ def plot_monthly_losses(
std = losses.std()
ax.plot(
losses * 100,
label=f"Electrical Losses\n$\mu$={mean:.2%}, $\sigma$={std:.2%}", # noqa: W605
label=f"Electrical Losses\n$\\mu$={mean:.2%}, $\\sigma$={std:.2%}", # noqa: W605
**plot_kwargs,
)

Expand Down
7 changes: 2 additions & 5 deletions openoa/analysis/turbine_long_term_gross_energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,7 @@ def __attrs_post_init__(self):
"""
Runs any non-automated setup steps for the analysis class.
"""
if (
set(("TurbineLongTermGrossEnergy", "all")).intersection(self.plant.analysis_type)
== set()
):
if {"TurbineLongTermGrossEnergy", "all"}.intersection(self.plant.analysis_type) == set():
self.plant.analysis_type.append("TurbineLongTermGrossEnergy")

# Ensure the data are up to spec before continuing with initialization
Expand Down Expand Up @@ -400,7 +397,7 @@ def setup_daily_reanalysis_data(self) -> None:

# Capture the runs reanalysis data set and ensure the U/V components exist
reanalysis_df = self.plant.reanalysis[self._run.reanalysis_product]
if len(set(("WMETR_HorWdSpdU", "WMETR_HorWdSpdV")).intersection(reanalysis_df.columns)) < 2:
if len({"WMETR_HorWdSpdU", "WMETR_HorWdSpdV"}.intersection(reanalysis_df.columns)) < 2:
(
reanalysis_df["WMETR_HorWdSpdU"],
reanalysis_df["WMETR_HorWdSpdV"],
Expand Down
4 changes: 2 additions & 2 deletions openoa/analysis/wake_losses.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,10 @@ def __attrs_post_init__(self):
logger.info("Initializing WakeLosses analysis object")

if self.wind_direction_data_type == "scada":
if set(("WakeLosses-scada", "all")).intersection(self.plant.analysis_type) == set():
if {"WakeLosses-scada", "all"}.intersection(self.plant.analysis_type) == set():
self.plant.analysis_type.append("WakeLosses-scada")
if self.wind_direction_data_type == "tower":
if set(("WakeLosses-tower", "all")).intersection(self.plant.analysis_type) == set():
if {"WakeLosses-tower", "all"}.intersection(self.plant.analysis_type) == set():
self.plant.analysis_type.append("WakeLosses-tower")

# Ensure the data are up to spec before continuing with initialization
Expand Down
2 changes: 1 addition & 1 deletion openoa/analysis/yaw_misalignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def __attrs_post_init__(self):
"""
Initialize logging and post-initialization setup steps.
"""
if set(("StaticYawMisalignment", "all")).intersection(self.plant.analysis_type) == set():
if {"StaticYawMisalignment", "all"}.intersection(self.plant.analysis_type) == set():
self.plant.analysis_type.append("StaticYawMisalignment")

# Ensure the data are up to spec before continuing with initialization
Expand Down
2 changes: 1 addition & 1 deletion openoa/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def _wrapper(self, *args, **kwargs):
def logged_function_call(the_function, msg="call"):
def _wrapper(*args, **kwargs):
logger = logging.getLogger(the_function.__module__)
logger.debug("{}: {}".format(the_function.__name__, msg))
logger.debug(f"{the_function.__name__}: {msg}")
return the_function(*args, **kwargs)

return _wrapper
Expand Down
12 changes: 6 additions & 6 deletions openoa/plant.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ def _analysis_filter(
for k in reanalysis_keys:
name = k.split("-")[1]
col_map = getattr(metadata, key)[name].col_map
_add[k] = set([col_map[v] for v in value])
_add[k] = {col_map[v] for v in value}
else:
col_map = getattr(metadata, key).col_map
column_requirements.update({key: set([col_map[v] for v in value])})
column_requirements.update({key: {col_map[v] for v in value}})
column_requirements.update(_add)

# Filter the missing columns, so only analysis-specific columns are provided
Expand Down Expand Up @@ -148,7 +148,7 @@ def _compose_error_message(
@logged_method_call
def frequency_validator(
actual_freq: str | int | float | None,
desired_freq: Optional[str | None | set[str]],
desired_freq: str | set[str] | None,
exact: bool,
) -> bool:
"""Helper function to check if the actual datetime stamp frequency is valid compared
Expand All @@ -173,11 +173,11 @@ def frequency_validator(
return False

if isinstance(desired_freq, str):
desired_freq = set([desired_freq])
desired_freq = {desired_freq}

# If an offset alias couldn't be found, then convert the desired frequency strings to seconds
if not isinstance(actual_freq, str):
desired_freq = set([ts.offset_to_seconds(el) for el in desired_freq])
desired_freq = {ts.offset_to_seconds(el) for el in desired_freq}

if exact:
return actual_freq in desired_freq
Expand Down Expand Up @@ -987,7 +987,7 @@ def _validate_frequency(self, category: str = "all") -> list[str]:
return invalid_freq

@logged_method_call
def validate(self, metadata: Optional[dict | str | Path | PlantMetaData] = None) -> None:
def validate(self, metadata: dict | str | Path | PlantMetaData | None = None) -> None:
"""Secondary method to validate the plant data objects after loading or changing
data with option to provide an updated `metadata` object/file as well
Expand Down
2 changes: 1 addition & 1 deletion openoa/utils/imputing.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def impute_data(
"""
final_col_name = deepcopy(target_col)
if data is None:
if any((not isinstance(x, pd.DataFrame) for x in (target_data, reference_data))):
if any(not isinstance(x, pd.DataFrame) for x in (target_data, reference_data)):
raise TypeError(
"If `data` is not provided, then `ref_data` and `target_data` must be provided as pandas DataFrames."
)
Expand Down
34 changes: 14 additions & 20 deletions openoa/utils/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1179,10 +1179,8 @@ def plot_wake_losses(
plot_norm_energy = True
else:
raise ValueError(
(
"The inputs `energy_data_por` and `energy_data_lt` must both have the same dimensions"
"as `efficiency_data_por` and `efficiency_data_lt`."
)
"The inputs `energy_data_por` and `energy_data_lt` must both have the same dimensions"
"as `efficiency_data_por` and `efficiency_data_lt`."
)
elif (energy_data_por is None) & (energy_data_lt is None):
plot_norm_energy = False
Expand Down Expand Up @@ -1511,18 +1509,16 @@ def plot_yaw_misalignment(
],
color=curve_fit_color_code,
linestyle="--",
label=f"Max. Power Vane Angle = {round(curve_fit_params_ws[:,i,1].mean(),1)}$^\circ$", # noqa: W605
label=rf"Max. Power Vane Angle = {round(curve_fit_params_ws[:,i,1].mean(),1)}$^\circ$", # noqa: W605
)

yaw_mis_mean = np.round(np.mean(yaw_misalignment_ws[:, i]), 1)
yaw_mis_lb = np.round(np.percentile(yaw_misalignment_ws[:, i], 2.5), 1)
yaw_mis_ub = np.round(np.percentile(yaw_misalignment_ws[:, i], 97.5), 1)

ax.set_title(
(
f"{ws} m/s\nYaw Misalignment = "
f"{yaw_mis_mean}$^\circ$ [{yaw_mis_lb}$^\circ$, {yaw_mis_ub}$^\circ$]" # noqa: W605
)
f"{ws} m/s\nYaw Misalignment = "
rf"{yaw_mis_mean}$^\circ$ [{yaw_mis_lb}$^\circ$, {yaw_mis_ub}$^\circ$]" # noqa: W605
)
else:
norm_factor = curve_fit_params_ws[i, 0]
Expand Down Expand Up @@ -1557,11 +1553,11 @@ def plot_yaw_misalignment(
],
color=curve_fit_color_code,
linestyle="--",
label=f"Max. Power Vane Angle = {round(curve_fit_params_ws[i,1],1)}$^\circ$", # noqa: W605
label=rf"Max. Power Vane Angle = {round(curve_fit_params_ws[i,1],1)}$^\circ$", # noqa: W605
)

ax.set_title(
f"{ws} m/s\nYaw Misalignment = {np.round(yaw_misalignment_ws[i],1)}$^\circ$" # noqa: W605
f"{ws} m/s\nYaw Misalignment = {np.round(yaw_misalignment_ws[i],1)}$^\\circ$" # noqa: W605
)

ax.plot(
Expand All @@ -1572,7 +1568,7 @@ def plot_yaw_misalignment(
],
color=mean_vane_color_code,
linestyle="--",
label=f"Mean Vane Angle = {round(mean_vane_angle_ws[i],1)}$^\circ$", # noqa: W605
label=rf"Mean Vane Angle = {round(mean_vane_angle_ws[i],1)}$^\circ$", # noqa: W605
)

ax.grid("on")
Expand All @@ -1598,28 +1594,26 @@ def plot_yaw_misalignment(
for i in range(len(ws_bins) % 3, 3):
axs[last_row][i].remove()
axs[last_row - 1][i].tick_params(labelbottom=True)
axs[last_row - 1][i].set_xlabel("Wind Vane Angle ($^\circ$)") # noqa: W605
axs[last_row - 1][i].set_xlabel(r"Wind Vane Angle ($^\circ$)") # noqa: W605

for i in range(len(ws_bins) % 3):
axs[last_row][i].set_xlabel("Wind Vane Angle ($^\circ$)") # noqa: W605
axs[last_row][i].set_xlabel(r"Wind Vane Angle ($^\circ$)") # noqa: W605
else:
for i in range(N_col):
axs[last_row][i].set_xlabel("Wind Vane Angle ($^\circ$)") # noqa: W605
axs[last_row][i].set_xlabel(r"Wind Vane Angle ($^\circ$)") # noqa: W605

mean_yaw_mis = np.round(np.mean(yaw_misalignment_ws), 1)
if UQ:
yaw_misalignment_95CI = np.round(
np.percentile(np.mean(yaw_misalignment_ws, 1), [2.5, 97.5]), 1
)
fig.suptitle(
(
f"Turbine {turbine_id}, Yaw Misalignment = {mean_yaw_mis}$^\circ$ " # noqa: W605
f"[{yaw_misalignment_95CI[0]}$^\circ$, {yaw_misalignment_95CI[1]}$^\circ$]" # noqa: W605
)
rf"Turbine {turbine_id}, Yaw Misalignment = {mean_yaw_mis}$^\circ$ " # noqa: W605
rf"[{yaw_misalignment_95CI[0]}$^\circ$, {yaw_misalignment_95CI[1]}$^\circ$]" # noqa: W605
)
else:
fig.suptitle(
f"Turbine {turbine_id}, Mean Yaw Misalignment = {str(mean_yaw_mis)}$^\circ$" # noqa: W605
rf"Turbine {turbine_id}, Mean Yaw Misalignment = {str(mean_yaw_mis)}$^\circ$" # noqa: W605
)

plt.tight_layout()
Expand Down
2 changes: 1 addition & 1 deletion openoa/utils/qa.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
set_styling()


def _remove_tz(df: pd.DataFrame, t_local_column: str) -> Tuple[np.ndarray, np.ndarray]:
def _remove_tz(df: pd.DataFrame, t_local_column: str) -> tuple[np.ndarray, np.ndarray]:
"""Identify the non-timestamp elements in the DataFrame timestamp column and return
a truth array for filtering the values and the timezone-naive timestamps.
Expand Down

0 comments on commit c0485c8

Please sign in to comment.