Skip to content

Commit

Permalink
Add pyupgrade to ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
berland committed Dec 10, 2024
1 parent f8d0b10 commit 434f5be
Show file tree
Hide file tree
Showing 20 changed files with 104 additions and 122 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ select = [
"NPY", # numpy specific rules
"PL", # pylint
"SIM", # flake-8-simplify
"UP", # pyupgrade
"W", # pycodestyle
]
ignore = [
Expand Down
5 changes: 2 additions & 3 deletions src/semeio/_docs_utils/_json_schema_2_rst.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from copy import deepcopy
from typing import List, Optional, Union


def _insert_ref(schema: dict, defs: dict) -> dict:
Expand Down Expand Up @@ -66,8 +65,8 @@ def _create_docs(schema: dict) -> str:


def _make_documentation(
schema: Union[list, dict, str],
required: Optional[List[str]] = None,
schema: list | dict | str,
required: list[str] | None = None,
level: int = 0,
preface: str = "",
element_seperator: str = "\n\n",
Expand Down
74 changes: 33 additions & 41 deletions src/semeio/fmudesign/_excel2dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ def _find_geninput_sheetname(input_filename):

if len(general_input_sheet) > 1:
raise ValueError(
"More than one sheet with general input. Sheetnames are {} ".format(
general_input_sheet
)
f"More than one sheet with general input. Sheetnames are {general_input_sheet} "
)

if not general_input_sheet:
Expand Down Expand Up @@ -120,9 +118,7 @@ def _find_onebyone_defaults_sheet(input_filename):
default_values_sheet.append(sheet)
if len(default_values_sheet) > 1:
raise ValueError(
"More than one sheet with default values. Sheetnames are {} ".format(
default_values_sheet
)
f"More than one sheet with default values. Sheetnames are {default_values_sheet} "
)

if len(default_values_sheet) == []:
Expand Down Expand Up @@ -175,9 +171,9 @@ def _check_designinput(dsgn_input):
if _has_value(row.sensname):
if row.sensname in sensitivity_names:
raise ValueError(
"sensname '{}' was found on more than one row in designinput "
f"sensname '{row.sensname}' was found on more than one row in designinput "
"sheet. Two sensitivities can not share the same sensname. "
"Please correct this and rerun".format(row.sensname)
"Please correct this and rerun"
)
sensitivity_names.append(row.sensname)

Expand All @@ -189,10 +185,10 @@ def _check_for_mixed_sensitivities(sens_name, sens_group):
types = sens_group.groupby("type", sort=False)
if len(types) > 1:
raise ValueError(
"The sensitivity with sensname '{}' in designinput sheet contains more "
f"The sensitivity with sensname '{sens_name}' in designinput sheet contains more "
"than one sensitivity type. For each sensname all parameters must be "
"specified using the same type (seed, scenario, dist, ref, background, "
"extern)".format(sens_name)
"extern)"
)


Expand Down Expand Up @@ -393,11 +389,9 @@ def _read_defaultvalues(filename, sheetname):
for row in default_df.itertuples():
if str(row[0]) in default_dict:
print(
"WARNING: The default value '{}' "
"is listed twice in the sheet '{}'. "
"Only the first entry will be used in output file".format(
row[0], sheetname
)
f"WARNING: The default value '{row[0]}' "
f"is listed twice in the sheet '{sheetname}'. "
"Only the first entry will be used in output file"
)
else:
default_dict[str(row[0])] = row[1]
Expand Down Expand Up @@ -432,9 +426,9 @@ def _read_dependencies(filename, sheetname, from_parameter):
depend_dict["to_params"][key] = depend_df[key].tolist()
else:
raise ValueError(
"Parameter {} specified to have derived parameters, "
"but the sheet specifying the dependencies {} does "
"not contain the input parameter. ".format(from_parameter, sheetname)
f"Parameter {from_parameter} specified to have derived parameters, "
f"but the sheet specifying the dependencies {sheetname} does "
"not contain the input parameter. "
)
return depend_dict

Expand Down Expand Up @@ -479,25 +473,25 @@ def _read_background(inp_filename, bck_sheet):
)
if not _has_value(row.dist_param1):
raise ValueError(
"Parameter {} has been input "
f"Parameter {row.param_name} has been input "
"in background sheet but with empty "
"first distribution parameter ".format(row.param_name)
"first distribution parameter "
)
if not _has_value(row.dist_param2) and _has_value(row.dist_param3):
raise ValueError(
"Parameter {} has been input in "
f"Parameter {row.param_name} has been input in "
"background sheet with "
'value for "dist_param3" while '
'"dist_param2" is empty. This is not '
"allowed".format(row.param_name)
"allowed"
)
if not _has_value(row.dist_param3) and _has_value(row.dist_param4):
raise ValueError(
"Parameter {} has been input in "
f"Parameter {row.param_name} has been input in "
"background sheet with "
'value for "dist_param4" while '
'"dist_param3" is empty. This is not '
"allowed".format(row.param_name)
"allowed"
)
distparams = [
item
Expand Down Expand Up @@ -545,15 +539,15 @@ def _read_scenario_sensitivity(sensgroup):
for row in sensgroup.itertuples():
if not _has_value(row.param_name):
raise ValueError(
"Scenario sensitivity {} specified "
f"Scenario sensitivity {row.sensname} specified "
"where one line has empty parameter "
"name ".format(row.sensname)
"name "
)
if not _has_value(row.value1):
raise ValueError(
"Parameter {} har been input "
f"Parameter {row.param_name} har been input "
'as type "scenario" but with empty '
"value in value1 column ".format(row.param_name)
"value in value1 column "
)
casedict1[str(row.param_name)] = row.value1

Expand Down Expand Up @@ -596,19 +590,17 @@ def _read_constants(sensgroup):
for row in sensgroup.itertuples():
if not _has_value(row.dist_param1):
raise ValueError(
"Parameter name {} has been input "
f"Parameter name {row.param_name} has been input "
'in a sensitivity of type "seed". \n'
"If {} was meant to be the name of "
f"If {row.param_name} was meant to be the name of "
"the seed parameter, this is "
"unfortunately not allowed. "
"The seed parameter name is standardised "
"to RMS_SEED and should not be specified.\n "
"If you instead meant to specify a constant "
"value for another parameter in the seed "
'sensitivity, please remember "const" in '
'dist_name and a value in "dist_param1". '.format(
row.param_name, row.param_name
)
'dist_name and a value in "dist_param1". '
)
distparams = row.dist_param1
paramdict[str(row.param_name)] = [str(row.dist_name), distparams]
Expand All @@ -631,29 +623,29 @@ def _read_dist_sensitivity(sensgroup):
for row in sensgroup.itertuples():
if not _has_value(row.param_name):
raise ValueError(
"Dist sensitivity {} specified "
f"Dist sensitivity {row.sensname} specified "
"where one line has empty parameter "
"name ".format(row.sensname)
"name "
)
if not _has_value(row.dist_param1):
raise ValueError(
"Parameter {} has been input "
f"Parameter {row.param_name} has been input "
'as type "dist" but with empty '
"first distribution parameter ".format(row.param_name)
"first distribution parameter "
)
if not _has_value(row.dist_param2) and _has_value(row.dist_param3):
raise ValueError(
"Parameter {} has been input with "
f"Parameter {row.param_name} has been input with "
'value for "dist_param3" while '
'"dist_param2" is empty. This is not '
"allowed".format(row.param_name)
"allowed"
)
if not _has_value(row.dist_param3) and _has_value(row.dist_param4):
raise ValueError(
"Parameter {} has been input with "
f"Parameter {row.param_name} has been input with "
'value for "dist_param4" while '
'"dist_param3" is empty. This is not '
"allowed".format(row.param_name)
"allowed"
)
distparams = [
item
Expand Down
4 changes: 2 additions & 2 deletions src/semeio/fmudesign/_tornado_onebyone.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def calc_tornadoinput(
avg1 = 0
print(
"Warning: Number of ok realizations is 0 in"
"sensitivity {} case1".format(sensname)
f"sensitivity {sensname} case1"
)

if designsummary.loc[sensno]["senstype"] == "mc":
Expand Down Expand Up @@ -227,7 +227,7 @@ def calc_tornadoinput(
avg2 = 0
print(
"Warning: Number of ok realizations is 0 in"
"sensitivity {} case2".format(sensname)
f"sensitivity {sensname} case2"
)
subset2name = designsummary.loc[sensno]["casename2"]
else:
Expand Down
43 changes: 20 additions & 23 deletions src/semeio/fmudesign/create_design.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
# are irrelevant and would only confuse users. We suppress them by redirecting
# stdout/stderr during import.
# https://github.com/cvxpy/cvxpy/issues/2470
with open(os.devnull, "w") as devnull, contextlib.redirect_stdout(
devnull
), contextlib.redirect_stderr(devnull):
with (
open(os.devnull, "w") as devnull,
contextlib.redirect_stdout(devnull),
contextlib.redirect_stderr(devnull),
):
import cvxpy as cp

import numpy as np
Expand Down Expand Up @@ -330,7 +332,7 @@ def to_xlsx(
print(
"Warning: Output filename did not have extension .xlsx "
"but the export format is Excel .xlsx . "
"Changing outputname to {}".format(filename)
f"Changing outputname to {filename}"
)

xlsxwriter = pd.ExcelWriter(filename, engine="openpyxl")
Expand Down Expand Up @@ -383,7 +385,7 @@ def add_seeds(self, seeds, max_reals):
"Valid choices for seeds are None, "
'"default" or an existing filename. '
"Neither was found in this case. seeds "
"had been specified as {} .".format(seeds)
f"had been specified as {seeds} ."
)

def add_background(
Expand Down Expand Up @@ -446,20 +448,16 @@ def _fill_with_background_values(self):
if len(temp_df) > len(self.backgroundvalues):
raise ValueError(
"Provided number of background values "
"{} is smaller than number"
" of realisations for sensitivity {}".format(
len(self.backgroundvalues), sensname
)
f"{len(self.backgroundvalues)} is smaller than number"
f" of realisations for sensitivity {sensname}"
)
elif len(temp_df) > len(self.backgroundvalues):
print(
"Provided number of background values "
"({}) is smaller than number"
" of realisations for sensitivity {}"
" and parameter {}. "
"Will be filled with default values.".format(
len(self.backgroundvalues), sensname, key
)
f"({len(self.backgroundvalues)}) is smaller than number"
f" of realisations for sensitivity {sensname}"
f" and parameter {key}. "
"Will be filled with default values."
)
existing_values = result_values.copy()
result_values = pd.concat([existing_values, temp_df])
Expand Down Expand Up @@ -490,13 +488,13 @@ def _fill_derived_params(self, depend_dict):
)
if self.designvalues[param].isnull().any():
raise ValueError(
"Column for derived parameter {} "
f"Column for derived parameter {param} "
"contains NaN. Check input "
"defining dependencies. "
"Could be Wrong values or that "
"values for input variable in "
"dependencies sheet "
"should be specified as strings.".format(param)
"should be specified as strings."
)

def _add_dist_background(
Expand Down Expand Up @@ -603,7 +601,7 @@ def generate(self, realnums, seedname, seedvalues, parameters):
raise ValueError(
'A sensitivity of type "seed" can only have '
"additional parameters where dist_name is "
'"const". Check sensitivity {}"'.format(self.sensname)
f'"const". Check sensitivity {self.sensname}"'
)
self.sensvalues[key] = constant

Expand Down Expand Up @@ -967,9 +965,9 @@ def generate(self, realnums, filename, parameters, seedvalues):
extern_values = _parameters_from_extern(filename)
if len(realnums) > len(extern_values):
raise ValueError(
"Number of realisations {} specified for "
"sensitivity {} is larger than rows in "
"file {}".format(len(realnums), self.sensname, filename)
f"Number of realisations {len(realnums)} specified for "
f"sensitivity {self.sensname} is larger than rows in "
f"file {filename}"
)
for param in parameters:
if param in extern_values:
Expand Down Expand Up @@ -1064,7 +1062,7 @@ def _printwarning(corr_group_name):
"Using designinput sheets where "
"corr_sheet is only specified for one parameter "
"will cause non-correlated parameters .\n"
"ONLY ONE PARAMETER WAS SPECIFIED TO USE CORR_SHEET {}\n"
f"ONLY ONE PARAMETER WAS SPECIFIED TO USE CORR_SHEET {corr_group_name}\n"
"\n"
"Note change in how correlated parameters are specified \n"
"from fmudeisgn version 1.0.1 in August 2019 :\n"
Expand All @@ -1079,5 +1077,4 @@ def _printwarning(corr_group_name):
"one-by-one-sensitivities\n"
"\n"
"####################################################\n"
"".format(corr_group_name)
)
9 changes: 3 additions & 6 deletions src/semeio/fmudesign/design_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import re
from pathlib import Path
from typing import Union

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -299,7 +298,7 @@ def draw_values_triangular(dist_parameters, numreals, rng, normalscoresamples=No
if high == low: # collapsed distribution
print(
"Low and high parameters for triangular distribution"
" are equal. Using constant {}".format(low)
f" are equal. Using constant {low}"
)
if normalscoresamples is not None:
values = scipy.stats.uniform.ppf(
Expand Down Expand Up @@ -355,7 +354,7 @@ def draw_values_pert(dist_parameters, numreals, rng, normalscoresamples=None):
if high == low: # collapsed distribution
print(
"Low and high parameters for pert distribution"
" are equal. Using constant {}".format(low)
f" are equal. Using constant {low}"
)
if normalscoresamples is not None:
values = scipy.stats.uniform.ppf(
Expand Down Expand Up @@ -515,9 +514,7 @@ def is_number(teststring):
return False


def read_correlations(
excel_filename: Union[str, Path], corr_sheet: str
) -> pd.DataFrame:
def read_correlations(excel_filename: str | Path, corr_sheet: str) -> pd.DataFrame:
"""Reading correlation info for a
monte carlo sensitivity
Expand Down
8 changes: 4 additions & 4 deletions src/semeio/fmudesign/fmudesignrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ def main():

if isinstance(args.config, str):
if not Path(args.config).is_file():
raise IOError(f"Input file {args.config} does not exist")
raise OSError(f"Input file {args.config} does not exist")
input_dict = excel2dict_design(args.config, sheetnames)

if args.config == args.destination:
raise IOError(
'Identical name "{}" have been provided for the input'
raise OSError(
f'Identical name "{args.config}" have been provided for the input'
"file and the output file. "
" Exiting.....".format(args.config)
" Exiting....."
)

design = DesignMatrix()
Expand Down
Loading

0 comments on commit 434f5be

Please sign in to comment.