Skip to content

Commit c18cb71

Browse files
authored
Merge pull request #60 from debpal/main
Method and variable name modifications
2 parents b2c62cc + 2bd183a commit c18cb71

16 files changed

+642
-537
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535

3636
## ✨ Key Features
3737

38-
- Modify parameters via the `calibration.cal` file.
39-
- Run SWAT+ simulations.
40-
- Perform sensitivity analysis on model parameters using the [SALib](https://github.com/SALib/SALib) Python package, with support for parallel computation.
41-
- Compute performance metrics using widely adopted indicators and derive Sobol sensitivity indices.
38+
- Modify model parameters through the `calibration.cal` file.
39+
- Run SWAT+ simulations seamlessly.
40+
- Compute performance metrics using widely adopted indicators.
41+
- Perform sensitivity analysis on model parameters using the [SALib](https://github.com/SALib/SALib) Python package, with support for parallel computation; currently, only Sobol sampling and Sobol indices are supported.
4242

4343

4444
## 📥 Install pySWATPlus

docs/changelog.md

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,34 @@
22

33
## Version 1.2.0 (Month DD, YYYY, not released yet)
44

5-
- Introduced the `pySWATPlus.DataManager` class with the following methods to support data processing workflows:
6-
7-
- `read_sensitive_dfs`: Reads sensitivity simulation data generated by the `simulation_by_sobol_sample` method in the `pySWATPlus.SensitivityAnalyzer` class.
8-
- `simulated_timeseries_df`: Moved from the `pySWATPlus.SensitivityAnalyzer` class for improved modularity.
5+
- All SWAT+ simulations with modified parameters are now configured through the `calibration.cal` file, eliminating the need to read and modify individual input files.
96

107
- Introduced the `pySWATPlus.PerformanceMetrics` class to compute performance metrics between simulated and observed values using the following indicators:
118

12-
- Nash–Sutcliffe Efficiency
13-
- Kling–Gupta Efficiency
14-
- Mean Squared Error
15-
- Root Mean Squared Error
16-
- Percent Bias
17-
- Mean Absolute Relative Error
9+
- Nash–Sutcliffe Efficiency
10+
- Kling–Gupta Efficiency
11+
- Mean Squared Error
12+
- Root Mean Squared Error
13+
- Percent Bias
14+
- Mean Absolute Relative Error
15+
16+
- Updated the `pySWATPlus.SensitivityAnalyzer` class:
1817

19-
- Added the `sobol_indices` method to the `pySWATPlus.SensitivityAnalyzer`** class for computing Sobol indices using the available indicators in the `pySWATPlus.PerformanceMetrics` class.
18+
- Renamed the method `simulation_by_sobol_sample` to `simulation_by_sample_parameters` to standardize naming and allow different sampling techniques in the future.
19+
- Added `parameter_sensitivity_indices` for computing sensitivity indices using the available indicators in the `pySWATPlus.PerformanceMetrics` class.
20+
21+
- Introduced the `pySWATPlus.DataManager` class with methods to support data processing workflows:
22+
23+
- `read_sensitive_dfs`: Reads sensitivity simulation data generated by the `simulation_by_sobol_sample` method in the `pySWATPlus.SensitivityAnalyzer` class.
24+
- `simulated_timeseries_df`: Moved from the `pySWATPlus.SensitivityAnalyzer` class to improve modularity.
2025

21-
- Added new methods to the `pySWATPlus.TxtinoutReader` class:
26+
- Updated the `pySWATPlus.TxtinoutReader` class:
2227

23-
- `set_simulation_timestep`: Modifies the simulation timestep in the `time.sim` file.
24-
- `set_print_interval`: Modifies the print interval in the `print.prt` file.
28+
- Added `set_simulation_timestep` to modify the simulation timestep in the `time.sim` file.
29+
- Added `set_print_interval` to modify the print interval in the `print.prt` file.
30+
- Added `set_print_period` to modify the print period in the `print.prt` file for recording simulated results.
31+
- Renamed `set_begin_and_end_date` to `set_simulation_period` for better consistency.
2532

26-
- All SWAT+ simulations with modified parameters are now configured through the `calibration.cal` file, eliminating the need to read and modify individual input files.
2733

2834

2935
## Version 1.1.0 (August 26, 2025)

docs/index.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
## ✨ Key Features
1010

11-
- Modify parameters via the `calibration.cal` file.
12-
- Run SWAT+ simulations.
13-
- Perform sensitivity analysis on model parameters using the [SALib](https://github.com/SALib/SALib) Python package, with support for parallel computation.
11+
- Modify model parameters through the `calibration.cal` file.
12+
- Run SWAT+ simulations seamlessly.
13+
- Compute performance metrics using widely adopted indicators.
14+
- Perform sensitivity analysis on model parameters using the [SALib](https://github.com/SALib/SALib) Python package, with support for parallel computation; currently, only Sobol sampling and Sobol indices are supported.
1415

1516

1617
## 📥 Install pySWATPlus

docs/userguide/data_analysis.md

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ print(output)
3232

3333
## Read Sensitivity Simulation Data
3434

35-
The sensitivity analysis performed using the [`simulation_by_sobol_sample`](https://swat-model.github.io/pySWATPlus/api/sensitivity_analyzer/#pySWATPlus.SensitivityAnalyzer.simulation_by_sobol_sample) method generates a file named `sensitivity_simulation.json` within the simulation directory.
36-
This JSON file contains all the information required for Sobol sensitivity analysis, including:
35+
The sensitivity analysis performed using the
36+
[`simulation_by_sample_parameters`](https://swat-model.github.io/pySWATPlus/api/sensitivity_analyzer/#pySWATPlus.SensitivityAnalyzer.simulation_by_sample_parameters) method generates a file named `sensitivity_simulation.json`. This JSON file contains all the information required for sensitivity analysis, including:
3737

38-
- `problem`: Sobol problem definition
38+
- `problem`: Problem definition dictionary
3939
- `sample`: List of generated samples
4040
- `simulation`: Simulated `DataFrame` corresponding to each sample
4141

4242
To retrieve the selected `DataFrame` for all scenarios, use:
4343

4444
```python
4545
output = pySWATPlus.DataManager().read_sensitive_dfs(
46-
sim_file=r"C:\Users\Username\simulation_folder\sensitivity_simulation.json",
46+
sensim_file=r"C:\Users\Username\simulation_folder\sensitivity_simulation.json",
4747
df_name='channel_sd_mon_df',
4848
add_problem=True,
4949
add_sample=True
@@ -65,7 +65,7 @@ To compute performance metrics for the desired indicators:
6565

6666
```python
6767
output = pySWATPlus.SensitivityAnalyzer().scenario_indicators(
68-
sim_file=r"C:\Users\Username\simulation_folder\sensitivity_simulation.json",
68+
sensim_file=r"C:\Users\Username\simulation_folder\sensitivity_simulation.json",
6969
df_name='channel_sd_mon_df',
7070
sim_col='flo_out',
7171
obs_file=r"C:\Users\Username\observed_folder\discharge_monthly.csv",
@@ -76,22 +76,7 @@ output = pySWATPlus.SensitivityAnalyzer().scenario_indicators(
7676
)
7777
```
7878

79-
## Sobol Indices
8079

81-
The available indicators can also be used to compute Sobol indices (first, second, and total orders) along with their confidence intervals.
82-
83-
```python
84-
output = pySWATPlus.SensitivityAnalyzer().sobol_indices(
85-
sim_file=r"C:\Users\Username\simulation_folder\sensitivity_simulation.json",
86-
df_name='channel_sd_mon_df',
87-
sim_col='flo_out',
88-
obs_file=r"C:\Users\Username\observed_folder\discharge_monthly.csv",
89-
date_format='%Y-%m-%d',
90-
obs_col='discharge',
91-
indicators=['KGE', 'RMSE'],
92-
json_file=r"C:\Users\Username\data_analysis\sobol_indices.json"
93-
)
94-
```
9580

9681

9782

docs/userguide/sensitivity_interface.md

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
# Sensitivity Analysis
1+
# Sensitivity Interface
22

3-
Sensitivity analysis helps quantify how variation in input parameters affects model outputs. This tutorial demonstrates how to perform sensitivity analysis on SWAT+ model parameters.
4-
The parameter sampling is handled by the [SALib](https://github.com/SALib/SALib) Python package using [Sobol](https://doi.org/10.1016/S0378-4754(00)00270-6) sampling from a defined parameter space.
3+
Sensitivity interface helps quantify how variation in input parameters affects model outputs. This tutorial demonstrates how to perform sensitivity analysis on SWAT+ model parameters.
54

65

76
## Configuration Settings
@@ -15,34 +14,34 @@ import pySWATPlus
1514

1615
# Initialize the project's TxtInOut folder
1716
txtinout_reader = pySWATPlus.TxtinoutReader(
18-
path=r"C:\Users\Username\project\Scenarios\Default\TxtInOut"
17+
tio_dir=r"C:\Users\Username\project\Scenarios\Default\TxtInOut"
1918
)
2019

21-
# Copy required files to an empty custom directory
22-
target_dir = r"C:\Users\Username\custom_folder"
23-
target_dir = txtinout_reader.copy_required_files(
24-
target_dir=target_dir
20+
# Copy required files to an empty simulation directory
21+
sim_dir = r"C:\Users\Username\custom_folder"
22+
sim_dir = txtinout_reader.copy_required_files(
23+
sim_dir=sim_dir
2524
)
2625

27-
# Initialize TxtinoutReader with the custom directory
28-
target_reader = pySWATPlus.TxtinoutReader(
29-
path=target_dir
26+
# Initialize TxtinoutReader with the simulation directory
27+
sim_reader = pySWATPlus.TxtinoutReader(
28+
tio_dir=sim_dir
3029
)
3130

3231
# Disable CSV file generation to save time
33-
target_reader.disable_csv_print()
32+
sim_reader.disable_csv_print()
3433

3534
# Disable daily time series in print.prt (saves time and space)
36-
target_reader.enable_object_in_print_prt(
35+
sim_reader.enable_object_in_print_prt(
3736
obj=None,
3837
daily=False,
3938
monthly=True,
4039
yearly=True,
4140
avann=True
4241
)
4342

44-
# Run a trial simulation to verify expected time series outputs
45-
target_reader.run_swat(
43+
# Set simulation period and run a trial simulation to verify expected time series outputs
44+
sim_reader.run_swat(
4645
begin_date='01-Jan-2010',
4746
end_date='31-Dec-2012',
4847
warmup=1,
@@ -51,14 +50,16 @@ target_reader.run_swat(
5150
} # enable daily time series for 'channel_sd'
5251
```
5352

54-
## Sobol-Based Interface
53+
## Sensitivity Simulation
5554

56-
This high-level interface builds on the above configuration to run sensitivity simulations using Sobol sampling. It includes:
55+
This high-level interface builds on the above configuration to run sensitivity simulations using sampling, powered by the [SALib](https://github.com/SALib/SALib) Python package.
56+
Currently, it supports [Sobol](https://doi.org/10.1016/S0378-4754(00)00270-6) sampling from a defined parameter space. The interface provides:
57+
58+
- Automatic generation of samples for the parameter space
59+
- Parallel computation to accelerate simulations
60+
- Output extraction with filtering options
61+
- Structured export of results for downstream analysis
5762

58-
- Automatic generation of Sobol samples for the parameter space
59-
- Parallel computation to speed up simulations
60-
- Output extraction with filtering options
61-
- Structured export of results for downstream analysis
6263

6364
```python
6465
# Sensitivity parameter space
@@ -78,7 +79,7 @@ parameters = [
7879
]
7980

8081
# Target data extraction from sensitivity simulation
81-
simulation_data = {
82+
extract_data = {
8283
'channel_sdmorph_yr.txt': {
8384
'has_units': True,
8485
'ref_day': 15,
@@ -97,13 +98,30 @@ simulation_data = {
9798

9899
# Sensitivity simulation
99100
if __name__ == '__main__':
100-
output = pySWATPlus.SensitivityAnalyzer().simulation_by_sobol_sample(
101+
output = pySWATPlus.SensitivityAnalyzer().simulation_by_sample_parameters(
101102
parameters=parameters,
102103
sample_number=1,
103-
simulation_folder=r"C:\Users\Username\simulation_folder",
104-
txtinout_folder=target_dir,
105-
simulation_data=simulation_data,
106-
clean_setup=True
104+
sensim_dir=r"C:\Users\Username\simulation_folder",
105+
txtinout_folder=sim_dir,
106+
extract_data=extract_data
107107
)
108108
print(output)
109+
```
110+
111+
## Sensitivity Indices
112+
113+
Sensitivity indices (first, second, and total orders) are computed using the indicators available in the `pySWATPlus.PerformanceMetrics` class, along with their confidence intervals.
114+
115+
116+
```python
117+
output = pySWATPlus.SensitivityAnalyzer().parameter_sensitivity_indices(
118+
sensim_file=r"C:\Users\Username\simulation_folder\sensitivity_simulation.json",
119+
df_name='channel_sd_mon_df',
120+
sim_col='flo_out',
121+
obs_file=r"C:\Users\Username\observed_folder\discharge_monthly.csv",
122+
date_format='%Y-%m-%d',
123+
obs_col='discharge',
124+
indicators=['KGE', 'RMSE'],
125+
json_file=r"C:\Users\Username\sensitivity_indices.json"
126+
)
109127
```

docs/userguide/swatplus_simulation.md

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ Once the `TxtInOut` folder is properly configured with the necessary input files
1212
import pySWATPlus
1313

1414
# Replace this with the path to your project's TxtInOut folder
15-
txtinout_folder = r"C:\Users\Username\project\Scenarios\Default\TxtInOut"
15+
txtinout_dir = r"C:\Users\Username\project\Scenarios\Default\TxtInOut"
1616

1717
txtinout_reader = pySWATPlus.TxtinoutReader(
18-
path=txtinout_folder
18+
tio_dir=txtinout_dir
1919
)
2020
```
2121

@@ -42,26 +42,26 @@ To keep your original `TxtInOut` folder unchanged, it is recommended to run `SWA
4242

4343
```python
4444
# Replace this with your desired empty custom directory
45-
target_dir = r"C:\Users\Username\custom_folder"
45+
sim_dir = r"C:\Users\Username\custom_folder"
4646

4747
# Ensure the required files are copied to the custom directory
4848
txtinout_reader.copy_required_files(
49-
target_dir=target_dir
49+
sim_dir=sim_dir
5050
)
5151
```
5252

5353
- Initialize `TxtinoutReader` class for the custom directory
5454

5555
```python
56-
target_reader = pySWATPlus.TxtinoutReader(
57-
path=target_dir
56+
sim_reader = pySWATPlus.TxtinoutReader(
57+
tio_dir=sim_dir
5858
)
5959
```
6060

6161
- Run simulation
6262

6363
```python
64-
target_reader.run_swat()
64+
sim_reader.run_swat()
6565
```
6666

6767
## Step-wise Configurations and Simulations
@@ -74,9 +74,9 @@ The following steps demonstrate how to configure parameters in a custom director
7474

7575
```python
7676
# Update timeline in `time.sim` file
77-
target_reader.set_simulation_period(
77+
sim_reader.set_simulation_period(
7878
begin_date='01-Jan-2012',
79-
end_date='31-Dec-2016',
79+
end_date='31-Dec-2016'
8080
)
8181
```
8282

@@ -93,7 +93,7 @@ The following steps demonstrate how to configure parameters in a custom director
9393

9494
```python
9595
# Ensure simulation outputs for `channel_sd` object in `print.prt` file
96-
target_reader.enable_object_in_print_prt(
96+
sim_reader.enable_object_in_print_prt(
9797
obj='channel_sd',
9898
daily=False,
9999
monthly=True,
@@ -102,12 +102,22 @@ The following steps demonstrate how to configure parameters in a custom director
102102
)
103103
```
104104

105-
- Set output print interval within the simulation period:
105+
- Set print interval within the simulation period:
106106

107107
```python
108-
# Set ouput print every other day
109-
target_reader.set_print_interval(
110-
interval=2
108+
# Set print interval in `print.prt` file
109+
sim_reader.set_print_interval(
110+
interval=1
111+
)
112+
```
113+
114+
- Set print period within the simulation timeline to record result in output files:
115+
116+
```python
117+
# Set print period in `print.prt` file
118+
sim_reader.set_print_period(
119+
begin_date='15-Jun-2012',
120+
end_date='15-Jun-2016'
111121
)
112122
```
113123

@@ -121,7 +131,7 @@ The following steps demonstrate how to configure parameters in a custom director
121131
'value': 0.5
122132
}
123133
]
124-
target_reader.run_swat(
134+
sim_reader.run_swat(
125135
parameters=parameters
126136
)
127137
```
@@ -148,13 +158,15 @@ parameters = [
148158

149159
# Run SWAT+ simulation from the original `TxtInOut` folder
150160
txtinout_reader.run_swat(
151-
target_dir=r"C:\Users\Username\custom_folder", # mandatory
161+
sim_dir=r"C:\Users\Username\custom_folder", # mandatory
152162
parameters=parameters, # optional
153-
begin_date='01-Jan-2012', # optional
154-
end_date= '31-Dec-2016', # optional
163+
begin_date='01-Jan-2012', # optional
164+
end_date= '31-Dec-2016', # optional
165+
simulation_timestep=0, # optional
155166
warmup=1, # optional
156167
print_prt_control={'channel_sd': {'daily': False}}, # optional
168+
print_begin_date='15-Jun-2012', # optional
169+
print_end_date='15-Jun-2016', # optional
157170
print_interval=1 # optional
158171
)
159172
```
160-

0 commit comments

Comments
 (0)