From 3ff32c4a3cdb3b09e63bf4d0230f2cf1448c1fef Mon Sep 17 00:00:00 2001 From: Jon Mease Date: Mon, 4 Feb 2019 06:07:23 -0500 Subject: [PATCH 1/3] Add core figure_factory import test --- plotly/tests/test_core/test_figure_factory.py | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 plotly/tests/test_core/test_figure_factory.py diff --git a/plotly/tests/test_core/test_figure_factory.py b/plotly/tests/test_core/test_figure_factory.py new file mode 100644 index 00000000000..782dec9efc1 --- /dev/null +++ b/plotly/tests/test_core/test_figure_factory.py @@ -0,0 +1,4 @@ +def test_figure_factory_import_does_not_break(): + # Even if we don't have the optional dependencies installed, importing + # figure_factory should not cause an exception + import plotly.figure_factory From b43c7268f7b2f7f56cba1c74aef9e299b57f4137 Mon Sep 17 00:00:00 2001 From: Jon Mease Date: Mon, 4 Feb 2019 06:14:48 -0500 Subject: [PATCH 2/3] Make numpy and scipy optional imports in ternary contour figure factory --- plotly/figure_factory/_ternary_contour.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/plotly/figure_factory/_ternary_contour.py b/plotly/figure_factory/_ternary_contour.py index 6a8079c9224..66dfa50463a 100644 --- a/plotly/figure_factory/_ternary_contour.py +++ b/plotly/figure_factory/_ternary_contour.py @@ -1,8 +1,9 @@ from __future__ import absolute_import -import numpy as np -from scipy.interpolate import griddata +from plotly import optional_imports from plotly.graph_objs import graph_objs as go -import warnings + +interpolate = optional_imports.get_module('scipy.interpolate') +np = optional_imports.get_module('numpy') def _pl_deep(): @@ -415,8 +416,8 @@ def _compute_grid(coordinates, values, tooltip_mode): gr_x = np.linspace(x_min, x_max, n_interp) gr_y = np.linspace(y_min, y_max, n_interp) grid_x, grid_y = np.meshgrid(gr_x, gr_y) - grid_z = griddata(cartes_coord_points[:2].T, values, (grid_x, grid_y), - method='cubic') + grid_z = interpolate.griddata( + cartes_coord_points[:2].T, values, (grid_x, grid_y), method='cubic') bar_coords = np.einsum('ik, kmn -> imn', invM, np.stack((grid_x, grid_y, np.ones(grid_x.shape)))) # invalidate the points outside of the reference triangle @@ -495,6 +496,14 @@ def create_ternary_contour(coordinates, values, pole_labels=['a', 'b', 'c'], fig = ff.create_ternary_contour(np.stack((a, b)), z, coloring='lines') """ + if np is None: + raise ImportError("""\ +The create_ternary_contour figure factory requires the numpy package""") + + if interpolate is None: + raise ImportError("""\ +The create_ternary_contour figure factory requires the scipy package""") + grid_z, gr_x, gr_y, tooltip = _compute_grid(coordinates, values, tooltip_mode) From 09ab616036909de2991f82f40f5e2ecb6b79c263 Mon Sep 17 00:00:00 2001 From: Jon Mease Date: Mon, 4 Feb 2019 06:24:10 -0500 Subject: [PATCH 3/3] numpy is required by all figure_factories --- plotly/figure_factory/__init__.py | 6 +++++- plotly/figure_factory/_ternary_contour.py | 6 +----- plotly/tests/test_core/test_figure_factory.py | 4 ---- 3 files changed, 6 insertions(+), 10 deletions(-) delete mode 100644 plotly/tests/test_core/test_figure_factory.py diff --git a/plotly/figure_factory/__init__.py b/plotly/figure_factory/__init__.py index b7dd72c21bd..04937a44973 100644 --- a/plotly/figure_factory/__init__.py +++ b/plotly/figure_factory/__init__.py @@ -3,7 +3,11 @@ from plotly import optional_imports # Require that numpy exists for figure_factory -import numpy +np = optional_imports.get_module('numpy') +if np is None: + raise ImportError("""\ +The figure factory module requires the numpy package""") + from plotly.figure_factory._2d_density import create_2d_density from plotly.figure_factory._annotated_heatmap import create_annotated_heatmap diff --git a/plotly/figure_factory/_ternary_contour.py b/plotly/figure_factory/_ternary_contour.py index 66dfa50463a..d74254f2854 100644 --- a/plotly/figure_factory/_ternary_contour.py +++ b/plotly/figure_factory/_ternary_contour.py @@ -2,8 +2,8 @@ from plotly import optional_imports from plotly.graph_objs import graph_objs as go +import numpy as np interpolate = optional_imports.get_module('scipy.interpolate') -np = optional_imports.get_module('numpy') def _pl_deep(): @@ -496,10 +496,6 @@ def create_ternary_contour(coordinates, values, pole_labels=['a', 'b', 'c'], fig = ff.create_ternary_contour(np.stack((a, b)), z, coloring='lines') """ - if np is None: - raise ImportError("""\ -The create_ternary_contour figure factory requires the numpy package""") - if interpolate is None: raise ImportError("""\ The create_ternary_contour figure factory requires the scipy package""") diff --git a/plotly/tests/test_core/test_figure_factory.py b/plotly/tests/test_core/test_figure_factory.py deleted file mode 100644 index 782dec9efc1..00000000000 --- a/plotly/tests/test_core/test_figure_factory.py +++ /dev/null @@ -1,4 +0,0 @@ -def test_figure_factory_import_does_not_break(): - # Even if we don't have the optional dependencies installed, importing - # figure_factory should not cause an exception - import plotly.figure_factory