Skip to content

Commit 2bd183a

Browse files
committed
Modified variable and function names for a stable release
1 parent 5fc2ec1 commit 2bd183a

6 files changed

Lines changed: 348 additions & 274 deletions

File tree

pySWATPlus/data_manager.py

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class DataManager:
1414

1515
def simulated_timeseries_df(
1616
self,
17-
target_file: str | pathlib.Path,
17+
sim_file: str | pathlib.Path,
1818
has_units: bool,
1919
begin_date: typing.Optional[str] = None,
2020
end_date: typing.Optional[str] = None,
@@ -29,7 +29,7 @@ def simulated_timeseries_df(
2929
A new `date` column is constructed using `datetime.date` objects from the `yr`, `mon`, and `day` columns.
3030
3131
Args:
32-
target_file (str | pathlib.Path): Path to the input file containing time series data generated by
32+
sim_file (str | pathlib.Path): Path to the input file containing time series data generated by
3333
the method [`run_swat`](https://swat-model.github.io/pySWATPlus/api/txtinout_reader/#pySWATPlus.TxtinoutReader.run_swat).
3434
The file must contain `yr`, `mon`, and `day` columns.
3535
@@ -72,13 +72,13 @@ def simulated_timeseries_df(
7272
)
7373

7474
# Absolute file path
75-
target_file = pathlib.Path(target_file).resolve()
75+
sim_file = pathlib.Path(sim_file).resolve()
7676

7777
# DataFrame from input file
78-
skip_rows = [0, 2] if has_units else [0]
79-
df = utils._load_file(
80-
path=target_file,
81-
skip_rows=skip_rows
78+
skiprows = [0, 2] if has_units else [0]
79+
df = utils._df_extract(
80+
input_file=sim_file,
81+
skiprows=skiprows
8282
)
8383

8484
# DataFrame columns
@@ -92,7 +92,7 @@ def simulated_timeseries_df(
9292
]
9393
if len(missing_cols) > 0:
9494
raise ValueError(
95-
f'Missing required time series columns "{missing_cols}" in file "{target_file.name}"'
95+
f'Missing required time series columns "{missing_cols}" in file "{sim_file.name}"'
9696
)
9797
df[date_col] = pandas.to_datetime(
9898
df[time_cols].rename(columns={'yr': 'year', 'mon': 'month'})
@@ -105,9 +105,9 @@ def simulated_timeseries_df(
105105

106106
# Fix reference day
107107
if ref_day is not None:
108-
if target_file.stem.endswith(('_day', '_subday')):
108+
if sim_file.stem.endswith(('_day', '_subday')):
109109
raise ValueError(
110-
f'Parameter "ref_day" is not applicable for daily or sub-daily time series in file "{target_file.name}" '
110+
f'Parameter "ref_day" is not applicable for daily or sub-daily time series in file "{sim_file.name}" '
111111
f'because it would assign the same day to all records within a month.'
112112
)
113113
df[date_col] = df[date_col].apply(
@@ -116,9 +116,9 @@ def simulated_timeseries_df(
116116

117117
# Fix reference month
118118
if ref_month is not None:
119-
if target_file.stem.endswith('_mon'):
119+
if sim_file.stem.endswith('_mon'):
120120
raise ValueError(
121-
f'Parameter "ref_month" is not applicable for monthly time series in file "{target_file.name}" '
121+
f'Parameter "ref_month" is not applicable for monthly time series in file "{sim_file.name}" '
122122
f'because it would assign the same month to all records within a year.'
123123
)
124124
df[date_col] = df[date_col].apply(
@@ -128,26 +128,26 @@ def simulated_timeseries_df(
128128
# Check if filtering by date removed all rows
129129
if df.empty:
130130
raise ValueError(
131-
f'No data found between "{begin_date}" and "{end_date}" in file "{target_file.name}"'
131+
f'No data found between "{begin_date}" and "{end_date}" in file "{sim_file.name}"'
132132
)
133133

134134
# Filter rows by dictionary criteria
135135
if apply_filter is not None:
136136
for col, val in apply_filter.items():
137137
if col not in df_cols:
138138
raise ValueError(
139-
f'Column "{col}" in apply_filter was not found in file "{target_file.name}"'
139+
f'Column "{col}" in apply_filter was not found in file "{sim_file.name}"'
140140
)
141141
if not isinstance(val, list):
142142
raise TypeError(
143-
f'Column "{col}" in apply_filter for file "{target_file.name}" must be a list, '
143+
f'Column "{col}" in apply_filter for file "{sim_file.name}" must be a list, '
144144
f'but got type "{type(val).__name__}"'
145145
)
146146
df = df.loc[df[col].isin(val)]
147147
# Check if filtering removed all rows
148148
if df.empty:
149149
raise ValueError(
150-
f'Filtering by column "{col}" with values "{val}" returned no rows in "{target_file.name}"'
150+
f'Filtering by column "{col}" with values "{val}" returned no rows in "{sim_file.name}"'
151151
)
152152

153153
# Reset DataFrame index
@@ -162,7 +162,7 @@ def simulated_timeseries_df(
162162
for col in usecols:
163163
if col not in df_cols:
164164
raise ValueError(
165-
f'Column "{col}" specified in "usecols" was not found in file "{target_file.name}"'
165+
f'Column "{col}" specified in "usecols" was not found in file "{sim_file.name}"'
166166
)
167167
retain_cols = [date_col] + usecols
168168

@@ -191,22 +191,23 @@ def simulated_timeseries_df(
191191

192192
def read_sensitive_dfs(
193193
self,
194-
sim_file: pathlib.Path,
194+
sensim_file: str | pathlib.Path,
195195
df_name: str,
196196
add_problem: bool = False,
197197
add_sample: bool = False
198198
) -> dict[str, typing.Any]:
199199
'''
200-
Read sensitivity simulation data generated by the [`simulation_by_sobol_sample`](https://swat-model.github.io/pySWATPlus/api/sensitivity_analyzer/#pySWATPlus.SensitivityAnalyzer.simulation_by_sobol_sample)
201-
method, and return a dictionary mapping each scenario integer to its corresponding `DataFrame`.
200+
Read sensitivity simulation data generated by the method
201+
[`simulation_by_sample_parameters`](https://swat-model.github.io/pySWATPlus/api/sensitivity_analyzer/#pySWATPlus.SensitivityAnalyzer.simulation_by_sample_parameters),
202+
and return a dictionary mapping each scenario integer to its corresponding `DataFrame`.
202203
203204
The returned dictionary may include the following keys:
204205
- `scenario` (default): A mapping between each scenario integer and its corresponding DataFrame.
205206
- `problem` (optional): The problem definition.
206207
- `sample` (optional): The sample list used in the sensitivity simulation.
207208
208209
Args:
209-
sim_file (str | pathlib.Path): Path to the `sensitivity_simulation.json` file generated by `simulation_by_sobol_sample`.
210+
sensim_file (str | pathlib.Path): Path to the `sensitivity_simulation.json` file generated by `simulation_by_sample_parameters`.
210211
211212
df_name (str): Name of the `DataFrame` within `sensitivity_simulation.json`.
212213
@@ -218,12 +219,24 @@ def read_sensitive_dfs(
218219
A dictionary with the following keys:
219220
220221
- `scenario` (default): A mapping between each scenario integer and its corresponding DataFrame.
221-
- `problem` (optional): The problem definition.
222+
- `problem` (optional): The definition dictionary passed to sampling.
222223
- `sample` (optional): The sample list used in the sensitivity simulation.
223224
'''
224225

226+
# Check input variables type
227+
validators._variable_origin_static_type(
228+
vars_types=typing.get_type_hints(
229+
obj=self.read_sensitive_dfs
230+
),
231+
vars_values=locals()
232+
)
233+
234+
# Absolute file path
235+
sensim_file = pathlib.Path(sensim_file).resolve()
236+
237+
# Sensitiivty output data
225238
output = utils._retrieve_sensitivity_output(
226-
sim_file=sim_file,
239+
sensim_file=sensim_file,
227240
df_name=df_name,
228241
add_problem=add_problem,
229242
add_sample=add_sample

pySWATPlus/performance_metrics.py

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ def compute_nse(
4848
obs_col (str): Name of the column containing observed values.
4949
'''
5050

51+
# Check input variables type
52+
validators._variable_origin_static_type(
53+
vars_types=typing.get_type_hints(
54+
obj=self.compute_nse
55+
),
56+
vars_values=locals()
57+
)
58+
5159
# Simulation values
5260
sim_arr = df[sim_col].astype(float)
5361

@@ -79,6 +87,14 @@ def compute_kge(
7987
obs_col (str): Name of the column containing observed values.
8088
'''
8189

90+
# Check input variables type
91+
validators._variable_origin_static_type(
92+
vars_types=typing.get_type_hints(
93+
obj=self.compute_kge
94+
),
95+
vars_values=locals()
96+
)
97+
8298
# Simulation values
8399
sim_arr = df[sim_col].astype(float)
84100

@@ -106,7 +122,7 @@ def compute_mse(
106122
obs_col: str
107123
) -> float:
108124
'''
109-
Calculate the Mean Squared Error metric between simulated and observed values
125+
Calculate the `Mean Squared Error` metric between simulated and observed values
110126
111127
Args:
112128
df (pandas.DataFrame): DataFrame containing at least two columns with simulated and observed values.
@@ -116,6 +132,14 @@ def compute_mse(
116132
obs_col (str): Name of the column containing observed values.
117133
'''
118134

135+
# Check input variables type
136+
validators._variable_origin_static_type(
137+
vars_types=typing.get_type_hints(
138+
obj=self.compute_mse
139+
),
140+
vars_values=locals()
141+
)
142+
119143
# Simulation values
120144
sim_arr = df[sim_col].astype(float)
121145

@@ -134,7 +158,7 @@ def compute_rmse(
134158
obs_col: str
135159
) -> float:
136160
'''
137-
Calculate the Root Mean Squared Error metric between simulated and observed values.
161+
Calculate the `Root Mean Squared Error` metric between simulated and observed values.
138162
139163
Args:
140164
df (pandas.DataFrame): DataFrame containing at least two columns with simulated and observed values.
@@ -144,6 +168,14 @@ def compute_rmse(
144168
obs_col (str): Name of the column containing observed values.
145169
'''
146170

171+
# Check input variables type
172+
validators._variable_origin_static_type(
173+
vars_types=typing.get_type_hints(
174+
obj=self.compute_rmse
175+
),
176+
vars_values=locals()
177+
)
178+
147179
# computer MSE error
148180
mse_value = self.compute_mse(
149181
df=df,
@@ -163,7 +195,7 @@ def compute_pbias(
163195
obs_col: str
164196
) -> float:
165197
'''
166-
Calculate the Percent Bias metric between simulated and observed values.
198+
Calculate the `Percent Bias` metric between simulated and observed values.
167199
168200
Args:
169201
df (pandas.DataFrame): DataFrame containing at least two columns with simulated and observed values.
@@ -173,6 +205,14 @@ def compute_pbias(
173205
obs_col (str): Name of the column containing observed values.
174206
'''
175207

208+
# Check input variables type
209+
validators._variable_origin_static_type(
210+
vars_types=typing.get_type_hints(
211+
obj=self.compute_pbias
212+
),
213+
vars_values=locals()
214+
)
215+
176216
# Simulation values
177217
sim_arr = df[sim_col].astype(float)
178218

@@ -191,7 +231,7 @@ def compute_mare(
191231
obs_col: str
192232
) -> float:
193233
'''
194-
Calculate the Mean Absolute Relative Error metric between simulated and observed values
234+
Calculate the `Mean Absolute Relative Error` metric between simulated and observed values
195235
196236
Args:
197237
df (pandas.DataFrame): DataFrame containing at least two columns with simulated and observed values.
@@ -201,6 +241,14 @@ def compute_mare(
201241
obs_col (str): Name of the column containing observed values.
202242
'''
203243

244+
# Check input variables type
245+
validators._variable_origin_static_type(
246+
vars_types=typing.get_type_hints(
247+
obj=self.compute_mare
248+
),
249+
vars_values=locals()
250+
)
251+
204252
# Simulation values
205253
sim_arr = df[sim_col].astype(float)
206254

@@ -214,7 +262,7 @@ def compute_mare(
214262

215263
def scenario_indicators(
216264
self,
217-
sim_file: str | pathlib.Path,
265+
sensim_file: str | pathlib.Path,
218266
df_name: str,
219267
sim_col: str,
220268
obs_file: str | pathlib.Path,
@@ -224,20 +272,20 @@ def scenario_indicators(
224272
json_file: typing.Optional[str | pathlib.Path] = None
225273
) -> dict[str, typing.Any]:
226274
'''
227-
Compute performance indicators for sample scenarios obtained using
228-
the [`simulation_by_sobol_sample`](https://swat-model.github.io/pySWATPlus/api/sensitivity_analyzer/#pySWATPlus.SensitivityAnalyzer.simulation_by_sobol_sample) method.
275+
Compute performance indicators for sample scenarios obtained using the method
276+
[`simulation_by_sample_parameters`](https://swat-model.github.io/pySWATPlus/api/sensitivity_analyzer/#pySWATPlus.SensitivityAnalyzer.simulation_by_sample_parameters).
229277
230278
Before computing the indicators, simulated and observed values are normalized using the formula `(v - min_v) / (max_v - min_v)`,
231279
where `min_v` and `max_v` represent the minimum and maximum of all simulated and observed values combined.
232280
233281
The method returns a dictionary with two keys:
234282
235-
- `problem`: The definition dictionary passed to Sobol sampling.
283+
- `problem`: The definition dictionary passed to sampling.
236284
- `indicator`: A `DataFrame` containing the `Scenario` column and one column per indicator,
237285
with scenario indices and corresponding indicator values.
238286
239287
Args:
240-
sim_file (str | pathlib.Path): Path to the `sensitivity_simulation.json` file produced by `simulation_by_sobol_sample`.
288+
sensim_file (str | pathlib.Path): Path to the `sensitivity_simulation.json` file produced by `simulation_by_sobol_sample`.
241289
242290
df_name (str): Name of the `DataFrame` within `sensitivity_simulation.json` from which to compute scenario indicators.
243291
@@ -284,7 +332,7 @@ def scenario_indicators(
284332
)
285333

286334
# Observed DataFrame
287-
obs_df = utils._df_observed(
335+
obs_df = utils._df_observe(
288336
obs_file=pathlib.Path(obs_file).resolve(),
289337
date_format=date_format,
290338
obs_col=obs_col
@@ -293,7 +341,7 @@ def scenario_indicators(
293341

294342
# Retrieve sensitivity output
295343
sensitivity_sim = utils._retrieve_sensitivity_output(
296-
sim_file=pathlib.Path(sim_file).resolve(),
344+
sensim_file=pathlib.Path(sensim_file).resolve(),
297345
df_name=df_name,
298346
add_problem=True,
299347
add_sample=False

0 commit comments

Comments
 (0)