From c90770f0ea882b186d492872d8be3ae33fb17ba3 Mon Sep 17 00:00:00 2001 From: Amedeo Ceruti Date: Fri, 27 Sep 2024 14:34:02 +0200 Subject: [PATCH] changed setup, updated readme, added tests --- README.md | 14 +++++------ environment.yml | 18 ++++---------- pyproject.toml | 58 +++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 4 +-- setup.py | 39 +++-------------------------- tests/__init__.py | 3 +++ tests/sts.py | 7 ++++-- tests/test_setup.py | 11 ++++++++ topotherm/__init__.py | 2 +- 9 files changed, 95 insertions(+), 61 deletions(-) create mode 100644 pyproject.toml create mode 100644 tests/__init__.py create mode 100644 tests/test_setup.py diff --git a/README.md b/README.md index a51ddff..748cad9 100644 --- a/README.md +++ b/README.md @@ -92,11 +92,10 @@ This can also be done with venv or equivalent. We recommend to install the dependencies with anaconda or mamba: -```conda +```mamba cd topotherm -conda create -n topotherm python -conda activate topotherm -pip install -e . +mamba env create -f environment.yml -n topotherm +mamba activate topotherm ``` ## Solver @@ -112,9 +111,9 @@ the documentation [here](https://support.gurobi.com/hc/en-us/articles/3600442902 You can try the code on smaller benchmarks with several open source solvers, such as SCIP. Other popular open-source options are COIN-OR's cbc or HiGHS. -```conda -conda activate topotherm -conda install -c conda-forge pyscipopt +```mamba +mamba activate topotherm +mamba install -c conda-forge pyscipopt ``` ## Usage @@ -137,6 +136,7 @@ changes, please open an issue first to discuss what you would like to change. To run the tests, use pytest. ```Python +pip install .[dev] pytest tests ``` diff --git a/environment.yml b/environment.yml index 48ff19e..de06c82 100644 --- a/environment.yml +++ b/environment.yml @@ -1,17 +1,9 @@ -name: topotherm +name: topotherm> channels: - - defaults - conda-forge + - defaults dependencies: - python>=3.9 - - numpy<2.0 - - pandas - - scipy - - networkx - - matplotlib - - pyomo - - pyarrow>=14.0.1 - - Pillow>=10.2.0 - - pytest - - pydantic - - pyyaml + - pip + - pip: + - -r requirements.txt diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..a96cd93 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,58 @@ +[build-system] +requires = ["setuptools >= 64"] +build-backend = "setuptools.build_meta" + +[project] + +name = "topotherm" +version = "0.2.0" +description = "A package to design district heating networks" +readme = "README.md" + +authors = [ + { name = "Jerry Lambert", email = "jerry.lambert@tum.de"}, + { name = "Amedeo Ceruti", email = "amedeo.ceruti@tum.de"} +] + +classifiers=[ + "License :: OSI Approved :: MIT License", + "Programming Language :: Python :: 3", + "Topic :: Scientific/Engineering :: District Heating", + ] + +keywords = [ + "district heating", + "optimization", + "Linear Programming", + "Mixed Integer Linear Programming" + ] + +license = {file = "LICENSE"} + +requires-python = ">=3.9" +dependencies = [ + "numpy", + "pandas", + "scipy", + "networkx", + "matplotlib", + "pyomo > 6.0", + "pyarrow >= 14.0.1", + "Pillow >= 10.2.0", + "pyyaml", + "pydantic", +] + +[tool.setuptools] +packages = { find = { where = ["topotherm"], exclude = ["tests", "docs"] } } + +[project.optional-dependencies] +dev = [ + "pytest", +] +docs = [ + "sphinx", + "sphinx-rtd-theme", + "sphinx-autodoc", + "sphinx-viewcode" +] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index cfb2e06..12bde6e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1,9 @@ -numpy<2.0 +numpy pandas scipy networkx matplotlib -pyomo +pyomo>6.0 pyarrow>=14.0.1 Pillow>=10.2.0 pytest diff --git a/setup.py b/setup.py index c1a8014..148b39b 100644 --- a/setup.py +++ b/setup.py @@ -1,38 +1,5 @@ """Set up topotherm from requirements.""" +from setuptools import setup -import os -from setuptools import setup, find_packages - - -# The directory containing this file -here = os.path.dirname(os.path.realpath(__file__)) - -# The text of the README file -with open(os.path.join(here, "README.md"), encoding='utf-8') as fid: - readme = fid.read() - -with open(os.path.join(here, "requirements.txt"), encoding='utf-8') as fid: - reqs = fid.read().splitlines() - -# This call to setup() does all the work -setup( - name="topotherm", - version="0.1.0", - description="A package to design district heating networks", - long_description=readme, - long_description_content_type="text/markdown", - url="https://github.com/jylambert/topotherm", - author="Jerry Lambert, Amedeo Ceruti", - author_email="jerry.lambert@tum.de, amedeo.ceruti@tum.de", - license="MIT", - classifiers=[ - "License :: OSI Approved :: MIT License", - "Programming Language :: Python :: 3", - "Topic :: Scientific/Engineering :: District Heating", - ], - packages=find_packages(exclude=("tests", "docs")), - include_package_data=True, - install_requires=reqs, - keywords=["district heating", "optimization", "Linear Programming", - "Mixed Integer Linear Programming"] -) +if __name__ == "__main__": + setup() diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..2d3e3d9 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,3 @@ +# https://stackoverflow.com/questions/54895002/modulenotfounderror-with-pytest +import sys +sys.path.append('.') diff --git a/tests/sts.py b/tests/sts.py index 2d80277..c3ac352 100644 --- a/tests/sts.py +++ b/tests/sts.py @@ -2,6 +2,7 @@ import pyomo.environ as pyo import pandas as pd +from pytest import approx import topotherm as tt @@ -54,7 +55,8 @@ def test_sts_forced(request): assert result.solver.status == pyo.SolverStatus.ok # assert that the objective value is less than 2% away from the expected # value - assert (abs(pyo.value(model.obj)) - 4.6259e+06) < 0.02 * 4.6259e+06 + assert abs(pyo.value(model.obj)) == approx(4.6259e+06, rel=0.02) + # assert (abs(pyo.value(model.obj)) - 4.6259e+06) < 0.02 * 4.6259e+06 def test_sts_eco(request): @@ -86,4 +88,5 @@ def test_sts_eco(request): result = opt.solve(model, tee=True) assert result.solver.status == pyo.SolverStatus.ok - assert (abs(pyo.value(model.obj)) - 4.01854e+04) < 0.02 * 4.01854e+04 + assert abs(pyo.value(model.obj)) == approx(4.01854e+04, rel=0.02) + # assert (abs(pyo.value(model.obj)) - 4.01854e+04) < 0.02 * 4.01854e+04 diff --git a/tests/test_setup.py b/tests/test_setup.py new file mode 100644 index 0000000..fbe98ec --- /dev/null +++ b/tests/test_setup.py @@ -0,0 +1,11 @@ +"""test_setup.py tests the correct setup of topotherm.""" + +from pytest import approx + +def test_import(): + import topotherm + + +def test_functionality(): + from topotherm import single_timestep + assert single_timestep.annuity(c_i=0.01, n=10) == approx(0.1055820766, rel=1e-2) diff --git a/topotherm/__init__.py b/topotherm/__init__.py index 08ef63d..499a354 100644 --- a/topotherm/__init__.py +++ b/topotherm/__init__.py @@ -1,4 +1,3 @@ -from . import plotting from . import utils from . import fileio from . import precalculation_hydraulic @@ -8,3 +7,4 @@ from . import multiple_timestep from . import sets from . import model_old +from . import plotting