Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
668c598
Merge pull request #1 from zupit-it/chore/update-to-python-311-and-la…
alessandro-mariotti-zupit Nov 24, 2023
0c6ed65
feat: add pre-commit and related ci
alessandro-mariotti-zupit Nov 24, 2023
25ba8e0
ci: add tests in PR
alessandro-mariotti-zupit Nov 24, 2023
d0c6760
ci: fix python version for tests
alessandro-mariotti-zupit Nov 24, 2023
411ee3c
ci: fix python version for tests
alessandro-mariotti-zupit Nov 24, 2023
554d5e4
ci: fix python version for tests
alessandro-mariotti-zupit Nov 24, 2023
932d995
ci: fix python version in CI
alessandro-mariotti-zupit Nov 24, 2023
40b9b9a
Merge pull request #2 from zupit-it/feat/add-pre-commit-and-ci
alessandro-mariotti-zupit Nov 24, 2023
8b2da3f
fix: fix setup.py dependecies
alessandro-mariotti-zupit Nov 24, 2023
66bced7
BugFix
dalmo1991 Nov 25, 2023
382c8dd
fix: add missing requirements_dev.txt
alessandro-mariotti-zupit Nov 27, 2023
7d34b61
Merge branch 'master' into feat/add-pre-commit-and-ci
alessandro-mariotti-zupit Nov 27, 2023
f7b8c43
Merge pull request #10 from zupit-it/feat/add-pre-commit-and-ci
dalmo1991 Nov 27, 2023
953121b
Fixed broken link
dalmo1991 Oct 5, 2024
75d93c3
Update components.rst
dalmo1991 Jun 2, 2025
941fac0
update locked package versions to >= for numpy, numba, pandas, matplo…
jarq6c Jun 8, 2026
80b95a1
use modern numba types for eager compliation function signatures
jarq6c Jun 8, 2026
6a7a144
add tests for python 3.12 and 3.13
jarq6c Jun 8, 2026
577387f
switch sep definition to raw strings to avoid SyntaxWarning for inval…
jarq6c Jun 8, 2026
2106f8c
fix indentation error
jarq6c Jun 8, 2026
b2c6b8e
Auto-format Numba signatures to satisfy project pre-commit rules
jarq6c Jun 8, 2026
6da8e79
Merge pull request #15 from jarq6c/numba-modernization
dalmo1991 Jul 3, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[flake8]
extend-ignore = E203
max-line-length = 120
exclude = doc,test,.git,.venv,__pycache__,venv
12 changes: 8 additions & 4 deletions .github/workflows/automatic_test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: unittest on push

on:
pull_request:
push:
branches:
- master
Expand All @@ -9,13 +10,16 @@ on:
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.11", "3.12", "3.13"]
steps:
- name: Prepare the repo
uses: actions/checkout@master
- name: Set up Python 3.11
uses: actions/setup-python@v1
uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: 3.11
python-version: ${{ matrix.python-version }}
- name: Install packages
run: pip install -r test/unittest/test_requirements.txt
- name: Test
Expand Down
16 changes: 16 additions & 0 deletions .github/workflows/pre-commit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: pre-commit

on:
pull_request:
push:
branches: [master]

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- uses: pre-commit/action@v3.0.1
2 changes: 1 addition & 1 deletion .github/workflows/publish_to_pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ jobs:
- name: Publish on PyPI
uses: pypa/gh-action-pypi-publish@master
with:
password: ${{ secrets.PYPI_ACCESS_TOKEN }}
password: ${{ secrets.PYPI_ACCESS_TOKEN }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
build/
dist/
superflexpy.egg-info/
**/.ipynb_checkpoints
**/.ipynb_checkpoints
22 changes: 22 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
hooks:
- id: flake8
exclude: ^(doc/|^test/)
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
args: ["--profile", "black", "--filter-files"]
- repo: https://github.com/psf/black
rev: 23.11.0
hooks:
- id: black
2 changes: 1 addition & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Format: Name Surname, Affiliation, e-mail

Marco Dal Molin, EAWAG, marco.dalmolin.1991@gmail.com
Fabrizio Fenicia, EAWAG, fabrizio.fenicia@eawag.ch
Dmitri Kavetski, University of Adelaide, dmitri.kavetski@adelaide.edu.au
Dmitri Kavetski, University of Adelaide, dmitri.kavetski@adelaide.edu.au
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ flexible, conceptual, distributed hydrological models.
Refer to the [documentation](https://superflexpy.readthedocs.io/) to learn to
use the SuperflexPy, [install](https://pypi.org/project/superflexpy/) the
package from here. [Examples](examples/) showing the basic usage of SuperflexPy
are available.
are available.

## Development

Please install all packages listed in the [requirements_dev.txt](requirements_dev.txt) file,
the run `pre-commit install` to install the git hooks. This will enable code formatting and linting on every git commit.
87 changes: 35 additions & 52 deletions doc/build_element_code.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import numba as nb
import numpy as np
from superflexpy.framework.element import ODEsElement
from superflexpy.framework.element import LagElement
from superflexpy.framework.element import ParameterizedElement

from superflexpy.framework.element import LagElement, ODEsElement, ParameterizedElement

# Implement a linear reservoir

Expand All @@ -27,22 +26,18 @@ class LinearReservoir(ODEsElement):
"""

def __init__(self, parameters, states, approximation, id):

ODEsElement.__init__(self,
parameters=parameters,
states=states,
approximation=approximation,
id=id)
ODEsElement.__init__(self, parameters=parameters, states=states, approximation=approximation, id=id)

self._fluxes_python = [self._fluxes_function_python] # Used by get fluxes, regardless of the architecture

if approximation.architecture == 'numba':
if approximation.architecture == "numba":
self._fluxes = [self._fluxes_function_numba]
elif approximation.architecture == 'python':
elif approximation.architecture == "python":
self._fluxes = [self._fluxes_function_python]
else:
message = '{}The architecture ({}) of the approximation is not correct'.format(self._error_message,
approximation.architecture)
message = "{}The architecture ({}) of the approximation is not correct".format(
self._error_message, approximation.architecture
)
raise ValueError(message)

def set_input(self, input):
Expand All @@ -57,7 +52,7 @@ def set_input(self, input):
1. Rainfall
"""

self.input = {'P': input[0]}
self.input = {"P": input[0]}

def get_output(self, solve=True):
"""
Expand All @@ -72,65 +67,58 @@ def get_output(self, solve=True):
"""

if solve:
self._solver_states = [self._states[self._prefix_states + 'S0']]
self._solver_states = [self._states[self._prefix_states + "S0"]]
self._solve_differential_equation()

# Update the state
self.set_states({self._prefix_states + 'S0': self.state_array[-1, 0]})

fluxes = self._num_app.get_fluxes(fluxes=self._fluxes_python,
S=self.state_array,
S0=self._solver_states,
dt=self._dt,
**self.input,
**{k[len(self._prefix_parameters):]: self._parameters[k] for k in self._parameters},
)
return [- fluxes[0][1]]
self.set_states({self._prefix_states + "S0": self.state_array[-1, 0]})

fluxes = self._num_app.get_fluxes(
fluxes=self._fluxes_python,
S=self.state_array,
S0=self._solver_states,
dt=self._dt,
**self.input,
**{k[len(self._prefix_parameters) :]: self._parameters[k] for k in self._parameters},
)
return [-fluxes[0][1]]

@staticmethod
def _fluxes_function_python(S, S0, ind, P, k, dt):

if ind is None:
return (
[
P,
- k * S,
-k * S,
],
0.0,
S0 + P * dt
S0 + P * dt,
)
else:
return (
[
P[ind],
- k[ind] * S,
-k[ind] * S,
],
0.0,
S0 + P[ind] * dt[ind],
[
0.0,
- k[ind]
]
[0.0, -k[ind]],
)

@staticmethod
@nb.jit('Tuple((UniTuple(f8, 2), f8, f8))(optional(f8), f8, i4, f8[:], f8[:], f8[:])',
nopython=True)
@nb.jit("Tuple((UniTuple(f8, 2), f8, f8))(optional(f8), f8, i4, f8[:], f8[:], f8[:])", nopython=True)
def _fluxes_function_numba(S, S0, ind, P, k, dt):

return (
(
P[ind],
- k[ind] * S,
-k[ind] * S,
),
0.0,
S0 + P[ind] * dt[ind],
(
0.0,
- k[ind]
)
(0.0, -k[ind]),
)


# Implement lag function


Expand All @@ -152,33 +140,31 @@ class TriangularLag(LagElement):
"""

def _build_weight(self, lag_time):

weight = []

for t in lag_time:
array_length = np.ceil(t)
w_i = []

for i in range(int(array_length)):
w_i.append(self._calculate_lag_area(i + 1, t)
- self._calculate_lag_area(i, t))
w_i.append(self._calculate_lag_area(i + 1, t) - self._calculate_lag_area(i, t))

weight.append(np.array(w_i))

return weight

@staticmethod
def _calculate_lag_area(bin, len):

if bin <= 0:
value = 0
elif bin < len:
value = (bin / len)**2
value = (bin / len) ** 2
else:
value = 1

return value


# Implement a parametrized splitter


Expand Down Expand Up @@ -214,7 +200,7 @@ def set_input(self, input):
1. Incoming flow
"""

self.input = {'Q_in': input[0]}
self.input = {"Q_in": input[0]}

def get_output(self, solve=True):
"""
Expand All @@ -233,9 +219,6 @@ def get_output(self, solve=True):

# solve is not needed but kept in the interface

split_par = self._parameters[self._prefix_parameters + 'split-par']
split_par = self._parameters[self._prefix_parameters + "split-par"]

return [
self.input['Q_in'] * split_par,
self.input['Q_in'] * (1 - split_par)
]
return [self.input["Q_in"] * split_par, self.input["Q_in"] * (1 - split_par)]
2 changes: 1 addition & 1 deletion doc/case_studies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,4 @@ We can now run the model and access its output (see :ref:`demo_network` for deta
.. literalinclude:: model_thur_hess2020.py
:language: python
:lines: 262
:linenos:
:linenos:
14 changes: 2 additions & 12 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
.. note:: Last update 25/11/2023
.. note:: Last update 29/11/2021

Change log
==========

Version 1.3.2
-------------

Minor changes
.............

- `Pull request <https://github.com/dalmo1991/superflexPy/pull/9>`_ from
`alessandro-mariotti-zupit <https://github.com/alessandro-mariotti-zupit>`_
that upgrades python and packages versions.

Version 1.3.1
-------------

Expand Down Expand Up @@ -126,4 +116,4 @@ Minor changes to existing components
New code
........

- Added :code:`hymod` elements
- Added :code:`hymod` elements
2 changes: 1 addition & 1 deletion doc/components.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ In the schematic, element E3 receives 3 input fluxes, which are indexed based
on their order: red (index 0), black (index 1), and blue (index 2). The red flux
comes from both upstream elements (index 0 and 1, respectively); the black flux
comes only from element E1 (index 1); the blue flux comes only from element E2
(index 2). This information is represented by the direction matrix
(index 0). This information is represented by the direction matrix
:math:`\mathbf{D}` as follows:

.. math::
Expand Down
8 changes: 4 additions & 4 deletions doc/components_code.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
e1 = Element(parameters={'p1': 0.1}, states={'S': 10.0})
e1 = Element(parameters={"p1": 0.1}, states={"S": 10.0})

u1 = Unit([e1])
u2 = Unit([e1])

e1.set_parameters({'e1_p1': 0.2})
u1.set_parameters({'u1_e1_p1': 0.3})
u2.set_parameters({'u2_e1_p1': 0.4})
e1.set_parameters({"e1_p1": 0.2})
u1.set_parameters({"u1_e1_p1": 0.3})
u2.set_parameters({"u2_e1_p1": 0.4})
Loading
Loading