neuron-explorer
Steering-first neuron explorer for Qwen3.5-4B: push every MLP neuron, keep every rollout, browse the results
Run, edit, read out and calibrate SWAT2012 models from Python
Developer toolsJupyter Notebook · PythonGitHub @allixenderOnlineOwner unconfirmed
Have you tried it? One tap is enough. No sign-in needed.
Owners of public repositories under a personal GitHub account can sign in to verify ownership and manage the introduction themselves. No administrator approval is needed.
Sign in, generate a verification file, and commit it to this repository’s default branch within 30 minutes. Files on other branches or forks do not count. Then check the file below to manage the project without administrator approval. Keep the file to retain your access.
If needed, you will go to sign-in. After signing in, return here and press your chosen verification button again.
Run /nomorevibe verify in your project folder to verify ownership and manage the introduction yourself.
Verify in one minuteAsk about other verification options
a set of python modules to work with the Soil and Water Assessment Tool (SWAT), including model run, programmatic config edits, simulation readouts and callibration with the fantastic SPOTPY package
Taken from the repository README
Loading the full README. The saved excerpt is shown below.
A set of Python modules to work with the Soil and Water Assessment Tool (SWAT2012), including model runs, programmatic edits of the model input files, readout of simulation outputs and calibration with the SPOTPY package.
swatpy requires Python 3.10 or newer. A SWAT2012 executable is not included; it is looked up on the PATH or in the model directory, or it can be set explicitly as model.swat_exec.
A model is a working copy of an ArcSWAT TxtInOut folder with a small metadata file (.swatmodel.json):Read the full README ↗A set of Python modules to work with the Soil and Water Assessment Tool (SWAT2012), including model runs, programmatic edits of the model input files, readout of simulation outputs and calibration with the SPOTPY package.
pip install swatpy # numpy and chardet only
pip install "swatpy[calibration]" # plus spotpy, scipy and pandas for the calibration scripts
pip install "swatpy[mpi]" # plus mpi4py for parallel spotpy samplers
swatpy requires Python 3.10 or newer. A SWAT2012 executable is not included; it is looked up on the PATH or in the model directory, or it can be set explicitly as model.swat_exec.
A model is a working copy of an ArcSWAT TxtInOut folder with a small metadata file (.swatmodel.json):
from swatpy import SwatModel, ReadOut
model = SwatModel.initFromTxtInOut("path/to/TxtInOut", copy=True, target_dir="work")
# later: model = SwatModel.loadModelFromDirectory("work")
model.enrichModelMeta() # simulation period and printed years from file.cio
# calibration parameters: <v|r|a>__<PARAM>__<file type>
model.setParameter("r__CN2__mgt", -0.1) # relative: CN2 * (1 - 0.1) in all .mgt files
model.setParameter("v__GW_DELAY__gw", 30.0) # replace
model.setParameter("a__SOL_AWC__sol", 0.02) # add, all soil layers
model.swat_exec = "/path/to/swat2012"
model.run(silent=True)
reach = ReadOut.rchOutputManipulator(["FLOW_OUT"], [1], "skip", True, 0, model.working_dir, iprint="month")
flow = reach.outValues["FLOW_OUT"][1]
The file manipulators in swatpy.FileEdit (bsn, gw, mgt, sub, hru, rte, sol, file.cio) read and write parameters at their fixed positions in the SWAT input files. Changes are always computed from the values read when the manipulator was created, so repeated calibration runs do not accumulate; changes of several parameters of the same file are all kept. The line endings of the files are preserved. The SWAT-CUP qualifiers of a parameter name (hydrologic group, soil texture, landuse, subbasin, slope; e.g. v__CANMX__hru______FRSD) are parsed but not evaluated, such a parameter is applied to all files of its type.
The output readers in swatpy.ReadOut take the column positions of the variables from the header line of output.rch, output.sub and output.hru, so a reduced selection of printed variables in file.cio is read correctly. The yearly summary rows of monthly output and the closing average-annual rows are skipped.
The folder data/scripts contains the calibration drivers used with SPOTPY on the University of Tartu HPC cluster (sequential and MPI), and data/params the corresponding parameter ranges.
Version 0.3.0 fixes several errors that affect calibration results produced with earlier versions:
finishChangePar() in between (as in the calibration scripts) overwrote each other, only the last parameter per file type was applied;iprint="month") dropped December and kept the yearly summary row instead;efficiency.nash() and agr() returned a masked value when the observations contain nodata;SOL_ZMX joined two lines of the .sol file, and SOL_ZMX was read without its first digit;file.cio values were written as decimals, and solManipulationCorrection doubled the clay content;output.hru was read from the wrong columns.The packaging moved from setup.py to pyproject.toml; spotpy, scipy and pandas are optional dependencies now.
The user and API documentation lives in docs/ and is published to GitHub Pages at https://allixender.github.io/swatpy/ (built by .github/workflows/docs.yml).
pip install -e ".[docs]" # mkdocs-material + mkdocstrings
mkdocs serve # live preview
pip install -e ".[dev]"
pytest -m "not swat" # unit tests on synthetic fixtures
SWATPY_TEST_DOWNLOAD=1 pytest -m swat -rs # real SWAT2012 project, and model runs on linux x86_64
pytest tests/test_fileedit.py -k sol # a subset
The unit tests use a small synthetic SWAT2012 model and synthetic output files in tests/data, generated by tests/data/make_fixtures.py in the fixed-width layout of SWAT2012 rev 637. The swat tests download the rev 637 demo project and Linux executable from SWATdata (GPL-3, therefore not included here) into ~/.cache/swatpy-tests; SWATPY_SWAT_EXE points them to another SWAT2012 executable.
Releases are published to PyPI with trusted publishing from GitHub Actions (.github/workflows/publish.yml):
__version__ in swatpy/__init__.py and the version in CITATION.cff;v<version>, which builds, checks and uploads the package to PyPI;This requires a trusted publisher for the project swatpy on PyPI (and TestPyPI) with the repository allixender/swatpy, the workflow publish.yml and the environment pypi (testpypi), as well as these two environments in the GitHub repository settings.
If you use swatpy, please cite it via the Zenodo DOI 10.5281/zenodo.6322023, see also CITATION.cff.