From 32ccca6a453f7fd7ef643441233d4b12b39334d0 Mon Sep 17 00:00:00 2001 From: Guilherme Date: Fri, 26 May 2023 05:05:21 +0200 Subject: [PATCH 1/8] MAINT: convert windrose ipywidgets and jsonpickle to optional dep --- docs/user/requirements.rst | 23 ++++++++++++++++------- requirements_test.txt | 3 ++- rocketpy/EnvironmentAnalysis.py | 28 +++++++++++++++++++++++++--- setup.py | 6 +++--- 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/docs/user/requirements.rst b/docs/user/requirements.rst index a6a5c07ef..c45c0eaf9 100644 --- a/docs/user/requirements.rst +++ b/docs/user/requirements.rst @@ -21,12 +21,9 @@ The following packages are needed in order to run RocketPy: - Scipy >= 1.0 - Matplotlib >= 3.0 - netCDF4 >= 1.4, < 1.6 for Python 3.7+, netCDF4 >= 1.6.2 for Python 3.11 -- windrose >= 1.6.8 - requests - pytz - simplekml -- ipywidgets >= 7.6.3 -- jsonpickle All of these packages, are automatically installed when RocketPy is installed using either ``pip`` or ``conda``. However, in case the user wants to install these packages manually, they can do so by following the instructions bellow. @@ -44,12 +41,9 @@ The packages needed can be installed via ``pip`` by running the following lines pip install "scipy>=1.0" pip install "matplotlib>=3.0" pip install "netCDF4>=1.6.2" - pip install "windrose >= 1.6.8" - pip install "ipywidgets>=7.6.3" pip install requests pip install pytz pip install simplekml - pip install jsonpickle Installing Required Packages Using ``conda`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -67,7 +61,7 @@ To update Scipy and install netCDF4 using Conda, the following code is used: Optional Packages ----------------- -Optionally, you can install timezonefinder to allow for automatic timezone detection when performing Enviornment Analysis. +Optionally, you can install timezonefinder to allow for automatic timezone detection when performing Environment Analysis. This can be done by running the following line of code in your preferred terminal: .. code-block:: shell @@ -77,6 +71,21 @@ This can be done by running the following line of code in your preferred termina Keep in mind that this package is not required to run RocketPy, but it can be useful if you want to perform Environment Analysis. Furthermore, timezonefinder can only be used with Python 3.8+. +Similar to timezonefinder, you can install other optional packages, including: + +- `windrose` : to allow for windrose plots when performing Environment Analysis, +- `ipywidgets` : to allow for gifs generation when performing Environment Analysis, +- `jsonpickle` : to allow for saving and loading of RocketPy objects. + +You can install these packages by running the following lines in your preferred terminal: + +.. code-block:: shell + + pip install "windrose>=1.6.8" + pip install "ipywidgets>=7.6.3" + pip install jsonpickle + + Useful Packages --------------- diff --git a/requirements_test.txt b/requirements_test.txt index 8dc868d6b..0ae7bd720 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -3,4 +3,5 @@ pytest-coverage black flake8-black pandas -numericalunits==1.25 \ No newline at end of file +numericalunits==1.25 +ipywidgets>=7.6.3 \ No newline at end of file diff --git a/rocketpy/EnvironmentAnalysis.py b/rocketpy/EnvironmentAnalysis.py index 8c49c37c3..e5315e7cb 100644 --- a/rocketpy/EnvironmentAnalysis.py +++ b/rocketpy/EnvironmentAnalysis.py @@ -10,8 +10,6 @@ import warnings from collections import defaultdict -import ipywidgets as widgets -import jsonpickle import matplotlib.ticker as mtick import netCDF4 import numpy as np @@ -22,7 +20,6 @@ from matplotlib.animation import FuncAnimation from matplotlib.animation import PillowWriter as ImageWriter from scipy import stats -from windrose import WindroseAxes from rocketpy.Environment import Environment from rocketpy.Function import Function @@ -1926,6 +1923,10 @@ def plot_wind_rose( ------- WindroseAxes """ + try: + from windrose import WindroseAxes + except ImportError: + raise ImportError("windrose package is required to plot wind roses.") ax = WindroseAxes.from_ax(fig=fig, rect=rect) ax.bar( wind_direction, @@ -2064,6 +2065,13 @@ def animate_average_wind_rose(self, figsize=(8, 8), filename="wind_rose.gif"): ------- Image : ipywidgets.widgets.widget_media.Image """ + try: + import ipywidgets as widgets + except ImportError: + raise ImportError( + "You need to install ipywidgets to use this function. " + "Try 'pip install ipywidgets'" + ) days = list(self.surfaceDataDict.keys()) hours = list(self.surfaceDataDict[days[0]].keys()) @@ -3252,6 +3260,14 @@ def load(self, filename="EnvAnalysisDict"): EnvironmentAnalysis object """ + try: + import jsonpickle + except ImportError: + raise ImportError( + "The jsonpickle module is required to load a previously saved" + + "Environment Analysis file. Please install it by using the" + + "command 'pip install jsonpickle'." + ) encoded_class = open(filename).read() return jsonpickle.decode(encoded_class) @@ -3267,6 +3283,12 @@ def save(self, filename="EnvAnalysisDict"): ------- None """ + try: + import jsonpickle + except ImportError: + raise ImportError( + "The jsonpickle module is required to save the Environment Analysis" + ) encoded_class = jsonpickle.encode(self) file = open(filename, "w") file.write(encoded_class) diff --git a/setup.py b/setup.py index 591ae3447..4481539de 100644 --- a/setup.py +++ b/setup.py @@ -31,15 +31,15 @@ "scipy>=1.0", "matplotlib>=3.0", netCDF4_requirement, - "windrose>=1.6.8", - "ipywidgets>=7.6.3", "requests", "pytz", "simplekml", - "jsonpickle", ], extras_require={ "timezonefinder": ["timezonefinder"], + "windrose": ["windrose>=1.6.8"], + "ipywidgets": ["ipywidgets>=7.6.3"], + "jsonpickle": ["jsonpickle"], }, maintainer="RocketPy Developers", author="Giovani Hidalgo Ceotto", From 8a45c7b896e5ef869ef2072b22e18e1e86f5e18a Mon Sep 17 00:00:00 2001 From: Guilherme Date: Sat, 27 May 2023 07:22:12 +0200 Subject: [PATCH 2/8] MAINT: refactor optional packages description --- docs/user/requirements.rst | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/docs/user/requirements.rst b/docs/user/requirements.rst index c45c0eaf9..7d453651a 100644 --- a/docs/user/requirements.rst +++ b/docs/user/requirements.rst @@ -61,26 +61,18 @@ To update Scipy and install netCDF4 using Conda, the following code is used: Optional Packages ----------------- -Optionally, you can install timezonefinder to allow for automatic timezone detection when performing Environment Analysis. -This can be done by running the following line of code in your preferred terminal: - -.. code-block:: shell - - pip install timezonefinder - -Keep in mind that this package is not required to run RocketPy, but it can be useful if you want to perform Environment Analysis. -Furthermore, timezonefinder can only be used with Python 3.8+. - -Similar to timezonefinder, you can install other optional packages, including: +Optionally, you can install the following packages to allow for additional functionality: +- `timezonefinder` : to allow for automatic timezone detection when performing Environment Analysis, - `windrose` : to allow for windrose plots when performing Environment Analysis, -- `ipywidgets` : to allow for gifs generation when performing Environment Analysis, +- `ipywidgets` : to allow for GIFs generation when performing Environment Analysis, - `jsonpickle` : to allow for saving and loading of RocketPy objects. You can install these packages by running the following lines in your preferred terminal: .. code-block:: shell + pip install timezonefinder pip install "windrose>=1.6.8" pip install "ipywidgets>=7.6.3" pip install jsonpickle From 636b3b7632ec8c475c9549b44f92b438832f1b45 Mon Sep 17 00:00:00 2001 From: Guilherme Date: Sat, 27 May 2023 07:27:21 +0200 Subject: [PATCH 3/8] GIT: rename requirements_test.txt to requirements-tests.txt --- .github/workflows/test_pytest.yaml | 34 +++++++++---------- .travis.yml | 10 +++--- ...rements_test.txt => requirements-tests.txt | 0 3 files changed, 22 insertions(+), 22 deletions(-) rename requirements_test.txt => requirements-tests.txt (100%) diff --git a/.github/workflows/test_pytest.yaml b/.github/workflows/test_pytest.yaml index ebd84b465..5a46e536c 100644 --- a/.github/workflows/test_pytest.yaml +++ b/.github/workflows/test_pytest.yaml @@ -15,20 +15,20 @@ jobs: - 3.7 - 3.11 steps: - - uses: actions/checkout@v2 - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v2 - with: - python-version: ${{ matrix.python-version }} - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install -r requirements_test.txt - - name: Build RocketPy - run: | - pip install -e . - - name: Test with pytest - run: | - pytest - cd rocketpy - pytest --doctest-modules \ No newline at end of file + - uses: actions/checkout@v2 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements-tests.txt + - name: Build RocketPy + run: | + pip install -e . + - name: Test with pytest + run: | + pytest + cd rocketpy + pytest --doctest-modules diff --git a/.travis.yml b/.travis.yml index 24cea5178..b2ec516aa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,13 @@ language: python python: -- '3.8' + - "3.8" install: -- make install -- pip install -r requirements_test.txt + - make install + - pip install -r requirements-tests.txt before_script: -- make verify-lint + - make verify-lint script: -- make test + - make test deploy: provider: pypi username: __token__ diff --git a/requirements_test.txt b/requirements-tests.txt similarity index 100% rename from requirements_test.txt rename to requirements-tests.txt From 0845cd9f8554c7aaa3c5c228ff2229d71e75fcc7 Mon Sep 17 00:00:00 2001 From: Guilherme Date: Sat, 27 May 2023 07:44:06 +0200 Subject: [PATCH 4/8] MAINT: Update specify EnvironmentAnalysis in the requirements.rst Co-authored-by: MateusStano <69485049+MateusStano@users.noreply.github.com> --- docs/user/requirements.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/user/requirements.rst b/docs/user/requirements.rst index 7d453651a..9f45be1de 100644 --- a/docs/user/requirements.rst +++ b/docs/user/requirements.rst @@ -61,7 +61,7 @@ To update Scipy and install netCDF4 using Conda, the following code is used: Optional Packages ----------------- -Optionally, you can install the following packages to allow for additional functionality: +Optionally, you can install the following packages to allow for additional functionalities when using the EnvironmentAnalysis class: - `timezonefinder` : to allow for automatic timezone detection when performing Environment Analysis, - `windrose` : to allow for windrose plots when performing Environment Analysis, From a20111d7fe1f3750385b24a5397e9a15faa8e5c9 Mon Sep 17 00:00:00 2001 From: Guilherme Date: Sun, 28 May 2023 18:47:39 +0200 Subject: [PATCH 5/8] ENH: a better usage of extra_require feature --- docs/user/requirements.rst | 25 ++++++++++------- requirements-tests.txt | 3 +-- rocketpy/EnvironmentAnalysis.py | 48 +++++++++------------------------ setup.py | 33 ++++++++++++++--------- 4 files changed, 48 insertions(+), 61 deletions(-) diff --git a/docs/user/requirements.rst b/docs/user/requirements.rst index 9f45be1de..c79f1dfb0 100644 --- a/docs/user/requirements.rst +++ b/docs/user/requirements.rst @@ -61,23 +61,28 @@ To update Scipy and install netCDF4 using Conda, the following code is used: Optional Packages ----------------- -Optionally, you can install the following packages to allow for additional functionalities when using the EnvironmentAnalysis class: +The EnvironmentAnalysis class requires a few extra packages to be installed. +In case you want to use this class, you will need to install the following packages: -- `timezonefinder` : to allow for automatic timezone detection when performing Environment Analysis, -- `windrose` : to allow for windrose plots when performing Environment Analysis, -- `ipywidgets` : to allow for GIFs generation when performing Environment Analysis, -- `jsonpickle` : to allow for saving and loading of RocketPy objects. +- `timezonefinder` : to allow for automatic timezone detection, +- `windrose` : to allow for windrose plots, +- `ipywidgets` : to allow for GIFs generation, +- `jsonpickle` : to allow for saving and loading of class instances. -You can install these packages by running the following lines in your preferred terminal: +You can install all these packages by simply running the following lines in your preferred terminal: .. code-block:: shell - pip install timezonefinder - pip install "windrose>=1.6.8" - pip install "ipywidgets>=7.6.3" - pip install jsonpickle + pip install rocketpy[env_analysis] +Alternatively, you can instal all extra packages by running the following line in your preferred terminal: + +.. code-block:: shell + + pip install rocketpy[all] + + Useful Packages --------------- diff --git a/requirements-tests.txt b/requirements-tests.txt index 0ae7bd720..8dc868d6b 100644 --- a/requirements-tests.txt +++ b/requirements-tests.txt @@ -3,5 +3,4 @@ pytest-coverage black flake8-black pandas -numericalunits==1.25 -ipywidgets>=7.6.3 \ No newline at end of file +numericalunits==1.25 \ No newline at end of file diff --git a/rocketpy/EnvironmentAnalysis.py b/rocketpy/EnvironmentAnalysis.py index e5315e7cb..40d048b48 100644 --- a/rocketpy/EnvironmentAnalysis.py +++ b/rocketpy/EnvironmentAnalysis.py @@ -20,11 +20,22 @@ from matplotlib.animation import FuncAnimation from matplotlib.animation import PillowWriter as ImageWriter from scipy import stats -from rocketpy.Environment import Environment +from rocketpy.Environment import Environment from rocketpy.Function import Function from rocketpy.units import convert_units +try: + import ipywidgets as widgets + import jsonpickle + from timezonefinder import TimezoneFinder + from windrose import WindroseAxes +except ImportError: + raise ImportError( + "At least one required module could not be imported." + + "The EnvironmentAnalysis requires additional dependencies." + + "Please install them by running 'pip install rocketpy[env_analysis]'." + ) class EnvironmentAnalysis: """Class for analyzing the environment. @@ -417,16 +428,6 @@ def __localize_input_dates(self): def __find_preferred_timezone(self): if self.preferred_timezone is None: - try: - from timezonefinder import TimezoneFinder - except ImportError: - raise ImportError( - "The timezonefinder package is required to automatically " - + "determine local timezone based on lat,lon coordinates. " - + "Please specify the desired timezone using the `timezone` " - + "argument when initializing the EnvironmentAnalysis class " - + "or install timezonefinder with `pip install timezonefinder`." - ) # Use local timezone based on lat lon pair tf = TimezoneFinder() self.preferred_timezone = pytz.timezone( @@ -1923,10 +1924,6 @@ def plot_wind_rose( ------- WindroseAxes """ - try: - from windrose import WindroseAxes - except ImportError: - raise ImportError("windrose package is required to plot wind roses.") ax = WindroseAxes.from_ax(fig=fig, rect=rect) ax.bar( wind_direction, @@ -2065,13 +2062,6 @@ def animate_average_wind_rose(self, figsize=(8, 8), filename="wind_rose.gif"): ------- Image : ipywidgets.widgets.widget_media.Image """ - try: - import ipywidgets as widgets - except ImportError: - raise ImportError( - "You need to install ipywidgets to use this function. " - "Try 'pip install ipywidgets'" - ) days = list(self.surfaceDataDict.keys()) hours = list(self.surfaceDataDict[days[0]].keys()) @@ -3260,14 +3250,6 @@ def load(self, filename="EnvAnalysisDict"): EnvironmentAnalysis object """ - try: - import jsonpickle - except ImportError: - raise ImportError( - "The jsonpickle module is required to load a previously saved" - + "Environment Analysis file. Please install it by using the" - + "command 'pip install jsonpickle'." - ) encoded_class = open(filename).read() return jsonpickle.decode(encoded_class) @@ -3283,12 +3265,6 @@ def save(self, filename="EnvAnalysisDict"): ------- None """ - try: - import jsonpickle - except ImportError: - raise ImportError( - "The jsonpickle module is required to save the Environment Analysis" - ) encoded_class = jsonpickle.encode(self) file = open(filename, "w") file.write(encoded_class) diff --git a/setup.py b/setup.py index 4481539de..187e71c38 100644 --- a/setup.py +++ b/setup.py @@ -23,23 +23,30 @@ # https://github.com/Unidata/netcdf4-python/issues/1179 netCDF4_requirement = "netCDF4>=1.6.2" +necessary_require = [ + "numpy>=1.0", + "scipy>=1.0", + "matplotlib>=3.0", + netCDF4_requirement, + "requests", + "pytz", + "simplekml", +] + +env_analysis_require = [ + "timezonefinder", + "windrose>=1.6.8", + "ipywidgets>=7.6.3", + "jsonpickle", +] + setuptools.setup( name="rocketpy", version="0.13.1", - install_requires=[ - "numpy>=1.0", - "scipy>=1.0", - "matplotlib>=3.0", - netCDF4_requirement, - "requests", - "pytz", - "simplekml", - ], + install_requires=necessary_require, extras_require={ - "timezonefinder": ["timezonefinder"], - "windrose": ["windrose>=1.6.8"], - "ipywidgets": ["ipywidgets>=7.6.3"], - "jsonpickle": ["jsonpickle"], + "env_analysis": env_analysis_require, + "all": necessary_require + env_analysis_require, }, maintainer="RocketPy Developers", author="Giovani Hidalgo Ceotto", From 35afedd9823b784c9b110b487a587aaa28b280ea Mon Sep 17 00:00:00 2001 From: Lint Action Date: Sun, 28 May 2023 16:48:23 +0000 Subject: [PATCH 6/8] Fix code style issues with Black --- rocketpy/EnvironmentAnalysis.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/rocketpy/EnvironmentAnalysis.py b/rocketpy/EnvironmentAnalysis.py index 40d048b48..936e839cc 100644 --- a/rocketpy/EnvironmentAnalysis.py +++ b/rocketpy/EnvironmentAnalysis.py @@ -32,10 +32,11 @@ from windrose import WindroseAxes except ImportError: raise ImportError( - "At least one required module could not be imported." + - "The EnvironmentAnalysis requires additional dependencies." + - "Please install them by running 'pip install rocketpy[env_analysis]'." - ) + "At least one required module could not be imported." + + "The EnvironmentAnalysis requires additional dependencies." + + "Please install them by running 'pip install rocketpy[env_analysis]'." + ) + class EnvironmentAnalysis: """Class for analyzing the environment. From 95dcc7a6a25f8b1aa5174eeda83957c015791a71 Mon Sep 17 00:00:00 2001 From: Guilherme Date: Sun, 28 May 2023 18:51:29 +0200 Subject: [PATCH 7/8] FIX: github workflow needed extra_require --- .github/workflows/test_pytest.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test_pytest.yaml b/.github/workflows/test_pytest.yaml index 5a46e536c..c5e665444 100644 --- a/.github/workflows/test_pytest.yaml +++ b/.github/workflows/test_pytest.yaml @@ -26,7 +26,7 @@ jobs: pip install -r requirements-tests.txt - name: Build RocketPy run: | - pip install -e . + pip install -e.[all] - name: Test with pytest run: | pytest From aacf2aad362e0aaf29ea04325e77d727dd6de4aa Mon Sep 17 00:00:00 2001 From: Guilherme Date: Sat, 10 Jun 2023 22:25:40 +0200 Subject: [PATCH 8/8] MAINT: catching ImportError in EnvironmentAnalysis.py Co-authored-by: phmbressan <87212571+phmbressan@users.noreply.github.com> --- rocketpy/EnvironmentAnalysis.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rocketpy/EnvironmentAnalysis.py b/rocketpy/EnvironmentAnalysis.py index b0b7b03dc..b82ebd34e 100644 --- a/rocketpy/EnvironmentAnalysis.py +++ b/rocketpy/EnvironmentAnalysis.py @@ -25,11 +25,11 @@ import jsonpickle from timezonefinder import TimezoneFinder from windrose import WindroseAxes -except ImportError: +except ImportError as error: raise ImportError( - "At least one required module could not be imported." - + "The EnvironmentAnalysis requires additional dependencies." - + "Please install them by running 'pip install rocketpy[env_analysis]'." + f"The following error was encountered while importing dependencies: '{error}'. " + "Please note that the EnvironmentAnalysis requires additional dependencies, " + "which can be installed by running 'pip install rocketpy[env_analysis]'." ) from .plots.environment_analysis_plots import _EnvironmentAnalysisPlots from .prints.environment_analysis_prints import _EnvironmentAnalysisPrints