From 34c01e0144a8f355d22e5698fe4a6e434eee1974 Mon Sep 17 00:00:00 2001 From: Anthony Volk Date: Thu, 24 Oct 2024 23:13:03 +0200 Subject: [PATCH 1/4] feat: Separate chart formatting packages from main install --- setup.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/setup.py b/setup.py index 9a14c87..7a6f0d6 100644 --- a/setup.py +++ b/setup.py @@ -10,12 +10,16 @@ license="MIT", packages=["microdf"], install_requires=[ - "matplotlib", - "matplotlib-label-lines", "numpy", "pandas", - "seaborn", ], - extras_require={"taxcalc": ["taxcalc"]}, + extras_require={ + "taxcalc": ["taxcalc"], + "charts": [ + "seaborn", + "matplotlib", + "matplotlib-label-lines" + ] + }, zip_safe=False, ) From eeab81a353febbd7baef30374b8d399f186cdf67 Mon Sep 17 00:00:00 2001 From: Anthony Volk Date: Thu, 24 Oct 2024 23:17:17 +0200 Subject: [PATCH 2/4] feat: Factor seaborn imports out of main package --- microdf/charts.py | 8 +++++++- microdf/style.py | 7 ++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/microdf/charts.py b/microdf/charts.py index cab747e..49a97c2 100644 --- a/microdf/charts.py +++ b/microdf/charts.py @@ -1,7 +1,6 @@ import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np -import seaborn as sns import microdf as mdf @@ -19,6 +18,13 @@ def quantile_pct_chg_plot(df1, df2, col1, col2, w1=None, w2=None, q=None): :returns: Axis. """ + try: + import seaborn as sns + except ImportError: + raise ImportError( + "The function you've called requires extra dependencies. Please install microdf with the 'charts' extra by running 'pip install microdf[charts]'" + ) + if q is None: q = np.arange(0.1, 1, 0.1) # Calculate weighted quantiles. diff --git a/microdf/style.py b/microdf/style.py index 3915bb8..0729e45 100644 --- a/microdf/style.py +++ b/microdf/style.py @@ -1,6 +1,5 @@ import matplotlib as mpl import matplotlib.font_manager as fm -import seaborn as sns TITLE_COLOR = "#212121" @@ -16,6 +15,12 @@ def set_plot_style(dpi: int = DPI): (200). :type dpi: int, optional """ + try: + import seaborn as sns + except ImportError: + raise ImportError( + "The function you've called requires extra dependencies. Please install microdf with the 'charts' extra by running 'pip install microdf[charts]'" + ) sns.set_style("white") From 65455ecfd2136f6aedb721cf016db82162bb532d Mon Sep 17 00:00:00 2001 From: Anthony Volk Date: Thu, 24 Oct 2024 23:22:43 +0200 Subject: [PATCH 3/4] fix: Conditionally import matplotlib --- microdf/chart_utils.py | 9 ++++++--- microdf/charts.py | 4 ++-- microdf/style.py | 6 ++---- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/microdf/chart_utils.py b/microdf/chart_utils.py index c680e97..e8196fc 100644 --- a/microdf/chart_utils.py +++ b/microdf/chart_utils.py @@ -1,6 +1,3 @@ -import matplotlib as mpl - - def dollar_format(suffix=""): """Dollar formatter for matplotlib. @@ -19,6 +16,12 @@ def currency_format(currency="USD", suffix=""): :returns: FuncFormatter. """ + try: + import matplotlib as mpl + except ImportError: + raise ImportError( + "The function you've called requires extra dependencies. Please install microdf with the 'charts' extra by running 'pip install microdf[charts]'" + ) prefix = {"USD": "$", "GBP": "£"}[currency] diff --git a/microdf/charts.py b/microdf/charts.py index 49a97c2..891065b 100644 --- a/microdf/charts.py +++ b/microdf/charts.py @@ -1,5 +1,3 @@ -import matplotlib as mpl -import matplotlib.pyplot as plt import numpy as np import microdf as mdf @@ -20,6 +18,8 @@ def quantile_pct_chg_plot(df1, df2, col1, col2, w1=None, w2=None, q=None): """ try: import seaborn as sns + import matplotlib as mpl + import matplotlib.pyplot as plt except ImportError: raise ImportError( "The function you've called requires extra dependencies. Please install microdf with the 'charts' extra by running 'pip install microdf[charts]'" diff --git a/microdf/style.py b/microdf/style.py index 0729e45..a3a1030 100644 --- a/microdf/style.py +++ b/microdf/style.py @@ -1,7 +1,3 @@ -import matplotlib as mpl -import matplotlib.font_manager as fm - - TITLE_COLOR = "#212121" AXIS_COLOR = "#757575" GRID_COLOR = "#eeeeee" # Previously lighter #f5f5f5. @@ -17,6 +13,8 @@ def set_plot_style(dpi: int = DPI): """ try: import seaborn as sns + import matplotlib as mpl + import matplotlib.font_manager as fm except ImportError: raise ImportError( "The function you've called requires extra dependencies. Please install microdf with the 'charts' extra by running 'pip install microdf[charts]'" From b29e75207707c58cc3fda684e6261660ff13c22d Mon Sep 17 00:00:00 2001 From: Anthony Volk Date: Fri, 25 Oct 2024 00:03:06 +0200 Subject: [PATCH 4/4] chore: Lint --- microdf/chart_utils.py | 4 +++- microdf/charts.py | 4 +++- microdf/style.py | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/microdf/chart_utils.py b/microdf/chart_utils.py index e8196fc..c9dbb14 100644 --- a/microdf/chart_utils.py +++ b/microdf/chart_utils.py @@ -20,7 +20,9 @@ def currency_format(currency="USD", suffix=""): import matplotlib as mpl except ImportError: raise ImportError( - "The function you've called requires extra dependencies. Please install microdf with the 'charts' extra by running 'pip install microdf[charts]'" + "The function you've called requires extra dependencies. " + + "Please install microdf with the 'charts' extra by running " + + "'pip install microdf[charts]'" ) prefix = {"USD": "$", "GBP": "£"}[currency] diff --git a/microdf/charts.py b/microdf/charts.py index 891065b..35ba8b7 100644 --- a/microdf/charts.py +++ b/microdf/charts.py @@ -22,7 +22,9 @@ def quantile_pct_chg_plot(df1, df2, col1, col2, w1=None, w2=None, q=None): import matplotlib.pyplot as plt except ImportError: raise ImportError( - "The function you've called requires extra dependencies. Please install microdf with the 'charts' extra by running 'pip install microdf[charts]'" + "The function you've called requires extra dependencies. " + + "Please install microdf with the 'charts' extra by running " + + "'pip install microdf[charts]'" ) if q is None: diff --git a/microdf/style.py b/microdf/style.py index a3a1030..512c6cb 100644 --- a/microdf/style.py +++ b/microdf/style.py @@ -17,7 +17,9 @@ def set_plot_style(dpi: int = DPI): import matplotlib.font_manager as fm except ImportError: raise ImportError( - "The function you've called requires extra dependencies. Please install microdf with the 'charts' extra by running 'pip install microdf[charts]'" + "The function you've called requires extra dependencies. " + + "Please install microdf with the 'charts' extra by running " + + "'pip install microdf[charts]'" ) sns.set_style("white")