Skip to content

Commit 3890b0e

Browse files
authored
Merge pull request #57 from debpal/main
Refactor Sensitivity Analysis Workflow
2 parents dcac2c7 + 9a3e379 commit 3890b0e

24 files changed

Lines changed: 1063 additions & 1436 deletions

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,12 @@
3131

3232
## 📦 About
3333

34-
`pySWATPlus` is an open-source Python package that provides a programmatic interface to the [SWAT+](https://swat.tamu.edu/software/plus/) model, allowing users to run simulations, modify input files, and streamline custom experimentation through the model’s `TxtInOut` folder.
35-
34+
`pySWATPlus` is an open-source Python package that provides a programmatic interface to the [SWAT+](https://swat.tamu.edu/software/plus/) model, allowing users to run simulations and conduct custom experiments.
3635

3736
## ✨ Key Features
3837

39-
- Navigate and read files in the SWAT+ `TxtInOut` folder.
40-
- Modify input parameters and save the updated files.
41-
- Run SWAT+ simulations either in the main `TxtInOut` folder or in a user-specified directory.
38+
- Modify parameters via the `calibration.cal` file.
39+
- Run SWAT+ simulations.
4240
- Perform sensitivity analysis on model parameters using the [SALib](https://github.com/SALib/SALib) Python package, with support for parallel computation.
4341

4442

docs/api/types.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
::: pySWATPlus.types.ParametersType
1+
::: pySWATPlus.types.ModifyType
2+
3+
::: pySWATPlus.types.BoundType
24

3-
::: pySWATPlus.types.ParametersBoundedType

docs/index.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.14889319.svg)](https://doi.org/10.5281/zenodo.14889319)
55

66

7-
`pySWATPlus` is an open-source Python package that provides a programmatic interface to the [SWAT+](https://swat.tamu.edu/software/plus/) model, allowing users to run simulations, modify input files, and streamline custom experimentation through the model’s `TxtInOut` folder.
8-
7+
`pySWATPlus` is an open-source Python package that provides a programmatic interface to the [SWAT+](https://swat.tamu.edu/software/plus/) model, allowing users to run simulations and conduct custom experiments.
98

109
## ✨ Key Features
1110

12-
- Navigate and read files in the SWAT+ `TxtInOut` folder.
13-
- Modify input parameters and save the updated files.
14-
- Run SWAT+ simulations either in the main `TxtInOut` folder or in a user-specified directory.
11+
- Modify parameters via the `calibration.cal` file.
12+
- Run SWAT+ simulations.
1513
- Perform sensitivity analysis on model parameters using the [SALib](https://github.com/SALib/SALib) Python package, with support for parallel computation.
1614

1715

docs/userguide/read_output.md

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,33 @@
33
This section explains how to read output data generated from `SWAT+` simulations.
44
It covers accessing results from both standard simulations and Sobol-based sensitivity analyses.
55

6-
---
76

87
## Read Time Series Data
98

10-
A standard `SWAT+` simulation generates TXT files with time series columns: `day`, `mon`, and `yr` for day, month, and year, respectively. To create a time series `DataFrame` that includes a new `date` column with `datetime.date` objects. Additionally, the method can optionally save the resulting DataFrame to a JSON file. This is controlled by the parameters `save_df` and `json_file`: setting `save_df=True` enables the saving process, and `json_file` specifies the path where the DataFrame will be written.
9+
A standard `SWAT+` simulation generates TXT files with time series columns: `day`, `mon`, and `yr` for day, month, and year, respectively.
10+
The following method creates a time series `DataFrame` that includes a new `date` column with `datetime.date` objects and save the resulting DataFrame to a JSON file.
1111

1212
```python
13+
import pySWATPlus
14+
1315
output = pySWATPlus.SensitivityAnalyzer.simulated_timeseries_df(
14-
data_file=r"C:\Users\Username\simulation_folder_2\channel_sd_mon.txt",
16+
data_file=r"C:\Users\Username\custom_folder\channel_sd_mon.txt",
1517
has_units=True,
16-
start_date='2014-06-01',
17-
apply_filter={'yr': [2014, 2015], 'gis_id': [561]},
18-
usecols=['gis_id', 'flo_out'],
19-
save_df=True,
18+
begin_date='01-Jun-2011',
19+
end_date='01-Jun-2013',
20+
ref_day=15,
21+
apply_filter={'name': ['cha561']},
22+
usecols=['name', 'flo_out'],
2023
json_file=r"C:\Users\Username\output_folder\tmp.json"
2124
)
25+
26+
print(output)
2227
```
2328

2429
## Read Sensitivity Simulation Data
2530

26-
The high-level [Sobol-Based Simulation Interface](https://swat-model.github.io/pySWATPlus/userguide/sensitivity_analysis/#sobol-based-simulation-interface) automates the sensitivity analysis workflow using Sobol samples. By default, it generates a file named `sensitivity_simulation_sobol.json` in the simulation folder. This JSON file contains all the information needed to analyze Sobol simulations, including:
31+
The [sensitivity analysis](https://swat-model.github.io/pySWATPlus/userguide/sensitivity_analysis/) generates a file called `sensitivity_simulation.json` within the simulation folder.
32+
This JSON file stores all information necessary for Sobol simulation analysis, including:
2733

2834
- `problem`: the Sobol problem definition
2935
- `sample`: the generated sample array
@@ -33,8 +39,8 @@ To retrieve and process this data for further analysis:
3339

3440
```python
3541
import json
36-
import numpy as np
37-
import pandas as pd
42+
import numpy
43+
import pandas
3844
import io
3945

4046
# Path to the JSON file generated by the Sobol interface
@@ -48,25 +54,23 @@ with open(json_file, 'r') as input_json:
4854
problem_dict = sensitivity_dict['problem']
4955

5056
# Retrieve the Sobol sample array
51-
sample_array = np.array(sensitivity_dict['sample'])
57+
sample_array = numpy.array(sensitivity_dict['sample'])
5258

53-
# Retrieve targeted simulation DataFrames
59+
# Retrieve targeted DataFrames from sensitivity simulations
5460
simulation_dict = sensitivity_dict['simulation']
55-
df_name = 'channel_sd_yr_df' # Example output
61+
df_name = 'channel_sd_mon_df' # Example output
5662
df_dict = {}
5763

5864
for sim_key, sim_val in simulation_dict.items():
5965
# Convert JSON string to pandas DataFrame
60-
df = pd.read_json(
66+
df = pandas.read_json(
6167
path_or_buf=io.StringIO(sim_val[df_name])
6268
)
6369
# Ensure 'date' column is in datetime.date format
6470
df['date'] = df['date'].dt.date
6571
df_dict[sim_key] = df
6672
```
6773

68-
> **Notes:** `df_dict` contains a `DataFrame` for each simulation key, which can be easily analyzed or plotted. Replace `'channel_sd_yr_df'` with any other output `DataFrame` as needed.
69-
7074

7175

7276

0 commit comments

Comments
 (0)