Skip to content

Commit

Permalink
Merge pull request #2 from zupit-it/feat/add-pre-commit-and-ci
Browse files Browse the repository at this point in the history
feat: add pre commit and ci
  • Loading branch information
alessandro-mariotti-zupit authored Nov 24, 2023
2 parents 932d995 + 554d5e4 commit 40b9b9a
Show file tree
Hide file tree
Showing 130 changed files with 2,528 additions and 2,846 deletions.
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
1 change: 1 addition & 0 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 Down
14 changes: 14 additions & 0 deletions .github/workflows/pre-commit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: pre-commit

on:
pull_request:
push:
branches: [master]

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
- uses: pre-commit/[email protected]
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, [email protected]
Fabrizio Fenicia, EAWAG, [email protected]
Dmitri Kavetski, University of Adelaide, [email protected]
Dmitri Kavetski, University of Adelaide, [email protected]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ 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.
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:
2 changes: 1 addition & 1 deletion doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,4 @@ Minor changes to existing components
New code
........

- Added :code:`hymod` elements
- Added :code:`hymod` elements
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})
33 changes: 16 additions & 17 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@
#
import os
import sys
sys.path.insert(0, os.path.abspath('../'))

sys.path.insert(0, os.path.abspath("../"))


# -- Project information -----------------------------------------------------

project = 'SuperflexPy'
copyright = '2021, Marco Dal Molin, Dmitri Kavetski, Fabrizio Fenicia'
author = 'Marco Dal Molin, Dmitri Kavetski, Fabrizio Fenicia'
project = "SuperflexPy"
copyright = "2021, Marco Dal Molin, Dmitri Kavetski, Fabrizio Fenicia"
author = "Marco Dal Molin, Dmitri Kavetski, Fabrizio Fenicia"

# The full version, including alpha/beta/rc tags
release = '1.3.0'
release = "1.3.0"


# -- General configuration ---------------------------------------------------
Expand All @@ -31,35 +32,33 @@
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.napoleon',
'sphinx.ext.viewcode',
"sphinx.ext.autodoc",
"sphinx.ext.napoleon",
"sphinx.ext.viewcode",
]

napoleon_numpy_docstring = True
autodoc_member_order = 'bysource'
autodoc_member_order = "bysource"

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
templates_path = ["_templates"]

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []
master_doc = 'index'
master_doc = "index"

# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'
html_theme_options = {
'logo_only': True
}
html_logo = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'pics', 'logo_inverted_2.PNG')
html_theme = "sphinx_rtd_theme"
html_theme_options = {"logo_only": True}
html_logo = os.path.join(os.path.abspath(os.path.dirname(__file__)), "pics", "logo_inverted_2.PNG")

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
html_static_path = ["_static"]
Loading

0 comments on commit 40b9b9a

Please sign in to comment.