Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REF: make ipywidgets optional dependency #132

Merged
merged 11 commits into from
Jun 30, 2021
9 changes: 2 additions & 7 deletions .github/workflows/unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ jobs:
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
environment-file: [ci/36-GIT.yaml, ci/36-PYPI.yaml, ci/37-GIT.yaml, ci/37-PYPI.yaml, ci/38-GIT.yaml, ci/38-PYPI.yaml]
exclude:
- environment-file: ci/36-PYPI.yaml
os: windows-latest
- environment-file: ci/36-GIT.yaml
os: windows-latest
environment-file: [ci/37-GIT.yaml, ci/37-PYPI.yaml, ci/38-GIT.yaml, ci/38-PYPI.yaml, ci/39-GIT.yaml, ci/39-PYPI.yaml]
defaults:
run:
shell: bash -l {0}
Expand Down Expand Up @@ -59,4 +54,4 @@ jobs:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml
name: splot-codecov

2 changes: 1 addition & 1 deletion ci/37-PYPI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
- esda
- geopandas>=0.4.0
- giddy
- ipywidgets
# - ipywidgets
- libpysal
- mapclassify
- matplotlib
Expand Down
2 changes: 1 addition & 1 deletion ci/36-GIT.yaml → ci/39-GIT.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
channels:
- conda-forge
dependencies:
- python=3.6
- python=3.9
# testing
- codecov
- pytest
Expand Down
2 changes: 1 addition & 1 deletion ci/36-PYPI.yaml → ci/39-PYPI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
channels:
- conda-forge
dependencies:
- python=3.6
- python=3.9
# testing
- codecov
- pytest
Expand Down
19 changes: 12 additions & 7 deletions splot/_viz_giddy_mpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import matplotlib.pyplot as plt
import matplotlib as mpl
from esda.moran import Moran_Local
import ipywidgets as widgets
from ipywidgets import interact, fixed
from giddy.directional import Rose

from ._viz_utils import moran_hot_cold_spots
Expand Down Expand Up @@ -108,7 +106,7 @@ def dynamic_lisa_heatmap(rose, p=0.05, ax=None, **kwargs):
>>> for year in range(1969, 2010):
... income_table[str(year) + '_rel'] = (
... income_table[str(year)] / income_table[str(year)].mean())

merge to one gdf

>>> gdf = df.merge(income_table,left_on='STATE_NAME',right_on='Name')
Expand Down Expand Up @@ -218,9 +216,9 @@ def dynamic_lisa_rose(rose, attribute=None, ax=None, **kwargs):
>>> for year in range(1969, 2010):
... income_table[str(year) + '_rel'] = (
... income_table[str(year)] / income_table[str(year)].mean())

merge to one gdf

>>> gdf = df.merge(income_table,left_on='STATE_NAME',right_on='Name')

retrieve spatial weights and data for two points in time
Expand Down Expand Up @@ -290,7 +288,7 @@ def dynamic_lisa_rose(rose, attribute=None, ax=None, **kwargs):
def _add_arrow(line, position=None, direction='right', size=15, color=None):
"""
add an arrow to a line.

Parameters
----------
line:
Expand Down Expand Up @@ -404,7 +402,7 @@ def dynamic_lisa_vectors(rose, ax=None,

xlim = [rose.Y.min(), rose.Y.max()]
ylim = [rose.wY.min(), rose.wY.max()]

if 'c' in kwargs.keys():
color = kwargs.pop('c', 'b')
can_insert_colorbar = False
Expand Down Expand Up @@ -656,6 +654,13 @@ def dynamic_lisa_composite_explore(rose, gdf, pattern='',
>>> # plt.show()

"""
try:
from ipywidgets import interact, fixed
except (ImportError, ModuleNotFoundError):
raise ImportError(
"`ipywidgets` package is required to use dynamic_lisa_composite_explore."
"You can install it using `conda install ipywidgets` or `pip install ipywidgets`."
)
coldict = {col: col for col in gdf.columns if
col.endswith(pattern)}
interact(_dynamic_lisa_widget_update,
Expand Down
42 changes: 26 additions & 16 deletions splot/tests/test_viz_bokeh.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,68 @@
from libpysal import examples
import geopandas as gpd
import esda
import pytest

from splot._bk import (plot_choropleth, lisa_cluster,
moran_scatterplot, plot_local_autocorrelation)
from splot._bk import (
plot_choropleth,
lisa_cluster,
moran_scatterplot,
plot_local_autocorrelation,
)


@pytest.mark.skip(reason="to be deprecated")
def test_plot_choropleth():
link = examples.get_path('columbus.shp')
link = examples.get_path("columbus.shp")
df = gpd.read_file(link)

w = Queen.from_dataframe(df)
w.transform = 'r'
w.transform = "r"

TOOLS = "tap,help"
fig = plot_choropleth(df, 'HOVAL', title='columbus',
reverse_colors=True, tools=TOOLS)
fig = plot_choropleth(
df, "HOVAL", title="columbus", reverse_colors=True, tools=TOOLS
)


@pytest.mark.skip(reason="to be deprecated")
def test_lisa_cluster():
link = examples.get_path('columbus.shp')
link = examples.get_path("columbus.shp")
df = gpd.read_file(link)

y = df['HOVAL'].values
y = df["HOVAL"].values
w = Queen.from_dataframe(df)
w.transform = 'r'
w.transform = "r"

moran_loc = esda.moran.Moran_Local(y, w)

TOOLS = "tap,reset,help"
fig = lisa_cluster(moran_loc, df, p=0.05, tools=TOOLS)


@pytest.mark.skip(reason="to be deprecated")
def test_moran_scatterplot():
link = examples.get_path('columbus.shp')
link = examples.get_path("columbus.shp")
df = gpd.read_file(link)

y = df['HOVAL'].values
y = df["HOVAL"].values
w = Queen.from_dataframe(df)
w.transform = 'r'
w.transform = "r"

moran_loc = esda.moran.Moran_Local(y, w)

fig = moran_scatterplot(moran_loc, p=0.05)


@pytest.mark.skip(reason="to be deprecated")
def test_plot_local_autocorrelation():
link = examples.get_path('columbus.shp')
link = examples.get_path("columbus.shp")
df = gpd.read_file(link)

y = df['HOVAL'].values
y = df["HOVAL"].values
w = Queen.from_dataframe(df)
w.transform = 'r'
w.transform = "r"

moran_loc = esda.moran.Moran_Local(y, w)

fig = plot_local_autocorrelation(moran_loc, df, 'HOVAL')
fig = plot_local_autocorrelation(moran_loc, df, "HOVAL")
74 changes: 48 additions & 26 deletions splot/tests/test_viz_giddy_mpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,99 @@
import numpy as np
import matplotlib.pyplot as plt
from pytest import raises
import pytest

from giddy.directional import Rose
from splot.giddy import (dynamic_lisa_heatmap,
dynamic_lisa_rose,
dynamic_lisa_vectors,
dynamic_lisa_composite)
try:
import ipywidgets

HAS_IPYWIDGETS = True
except (ImportError, ModuleNotFoundError):
HAS_IPYWIDGETS = False


def _data_generation():
from giddy.directional import Rose

# get csv and shp
shp_link = examples.get_path('us48.shp')
shp_link = examples.get_path("us48.shp")
df = gpd.read_file(shp_link)
income_table = pd.read_csv(examples.get_path("usjoin.csv"))
# calculate relative values
for year in range(1969, 2010):
income_table[str(year) + '_rel'] = (
income_table[str(year)] / income_table[str(year)].mean())
income_table[str(year) + "_rel"] = (
income_table[str(year)] / income_table[str(year)].mean()
)
# merge
gdf = df.merge(income_table,left_on='STATE_NAME',right_on='Name')
gdf = df.merge(income_table, left_on="STATE_NAME", right_on="Name")
# retrieve spatial weights and data for two points in time
w = Queen.from_dataframe(gdf)
w.transform = 'r'
y1 = gdf['1969_rel'].values
y2 = gdf['2000_rel'].values
#calculate rose Object
w.transform = "r"
y1 = gdf["1969_rel"].values
y2 = gdf["2000_rel"].values
# calculate rose Object
Y = np.array([y1, y2]).T
rose = Rose(Y, w, k=5)
return gdf, y1, rose


def test_dynamic_lisa_heatmap():
from splot.giddy import dynamic_lisa_heatmap

_, _, rose = _data_generation()
fig, _ = dynamic_lisa_heatmap(rose)
plt.close(fig)
fig2, _ = dynamic_lisa_heatmap(rose, cmap='GnBu')

fig2, _ = dynamic_lisa_heatmap(rose, cmap="GnBu")
plt.close(fig2)


def test_dynamic_lisa_rose():
from splot.giddy import dynamic_lisa_rose

_, y1, rose = _data_generation()
fig1, _ = dynamic_lisa_rose(rose)
plt.close(fig1)

fig2, _ = dynamic_lisa_rose(rose, attribute=y1)
plt.close(fig2)
fig3, _ = dynamic_lisa_rose(rose, c='r')

fig3, _ = dynamic_lisa_rose(rose, c="r")
plt.close(fig3)

raises(ValueError, dynamic_lisa_rose,
rose, attribute=y1, color='blue')

raises(ValueError, dynamic_lisa_rose, rose, attribute=y1, color="blue")


def test_dynamic_lisa_vectors():
from splot.giddy import dynamic_lisa_vectors

_, _, rose = _data_generation()
fig1, _ = dynamic_lisa_vectors(rose)
plt.close(fig1)

fig2, _ = dynamic_lisa_vectors(rose, arrows=False)
plt.close(fig2)
fig3, _ = dynamic_lisa_vectors(rose, c='r')

fig3, _ = dynamic_lisa_vectors(rose, c="r")
plt.close(fig3)
fig4, axs = plt.subplots(1,3)
dynamic_lisa_vectors(rose, ax=axs[0], color='r')

fig4, axs = plt.subplots(1, 3)
dynamic_lisa_vectors(rose, ax=axs[0], color="r")
plt.close(fig4)


def test_dynamic_lisa_composite():
from splot.giddy import dynamic_lisa_composite

gdf, _, rose = _data_generation()
fig, _ = dynamic_lisa_composite(rose, gdf)
plt.close(fig)


@pytest.mark.skipif(HAS_IPYWIDGETS, reason="ipywidgets available")
def test_import_ipywidgets_error():
with pytest.raises(ImportError, match="`ipywidgets` package is required"):
from splot.giddy import dynamic_lisa_composite_explore

gdf, _, rose = _data_generation()

dynamic_lisa_composite_explore(rose, gdf)