Skip to content

Commit

Permalink
[BUG] set viz defaults for LineStrings in lisa_cluster (#140)
Browse files Browse the repository at this point in the history
* [BUG] add check for `geom_type` in `esda.lisa_cluster`

* different plotting defaults dependent on geom_type

* [TST] add test for LineString adaption in `lisa_cluster`

* [ENH] test for Polygon and MultiPolygon

Co-authored-by: Martin Fleischmann <[email protected]>

* [BUG] check entire gdf for Polygons

* .shape --> .shp

* fix linestring test

Co-authored-by: Martin Fleischmann <[email protected]>
Co-authored-by: James Gaboardi <[email protected]>
  • Loading branch information
3 people authored Sep 27, 2021
1 parent 22039aa commit b30f13e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 25 deletions.
19 changes: 13 additions & 6 deletions splot/_viz_esda_mpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,11 +930,18 @@ def lisa_cluster(moran_loc, gdf, p=0.05, ax=None,
fig, ax = plt.subplots(1, figsize=figsize)
else:
fig = ax.get_figure()

gdf.assign(cl=labels).plot(column='cl', categorical=True,
k=2, cmap=hmap, linewidth=0.1, ax=ax,
edgecolor='white', legend=legend,
legend_kwds=legend_kwds, **kwargs)

# check for Polygon, else no edgecolor
if gdf.geom_type.isin(['Polygon', 'MultiPolygon']).any():
gdf.assign(cl=labels).plot(column='cl', categorical=True,
k=2, cmap=hmap, linewidth=0.1, ax=ax,
edgecolor='white', legend=legend,
legend_kwds=legend_kwds, **kwargs)
else:
gdf.assign(cl=labels).plot(column='cl', categorical=True,
k=2, cmap=hmap, linewidth=1.5, ax=ax,
legend=legend,
legend_kwds=legend_kwds, **kwargs)
ax.set_axis_off()
ax.set_aspect('equal')
return fig, ax
Expand Down Expand Up @@ -1355,4 +1362,4 @@ def moran_facet(moran_matrix, figsize=(16,12),

axarr[row, col].set_title('')
plt.tight_layout()
return fig, axarr
return fig, axarr
54 changes: 35 additions & 19 deletions splot/tests/test_viz_esda_mpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,27 @@
_moran_bv_scatterplot,
_moran_loc_bv_scatterplot)


def _test_data():
guerry = examples.load_example('Guerry')
link_to_data = guerry.get_path('guerry.shp')
gdf = gpd.read_file(link_to_data)
return gdf


def _test_data_columbus():
columbus = examples.load_example('Columbus')
link_to_data = columbus.get_path('columbus.shp')
df = gpd.read_file(link_to_data)
return df


def _test_LineString():
link_to_data = examples.get_path('streets.shp')
gdf = gpd.read_file(link_to_data)
return gdf


def test_moran_scatterplot():
gdf = _test_data()
x = gdf['Suicids'].values
Expand Down Expand Up @@ -160,9 +175,7 @@ def test_plot_moran_bv():


def test_moran_loc_scatterplot():
columbus = examples.load_example('Columbus')
link_to_data = columbus.get_path('columbus.shp')
df = gpd.read_file(link_to_data)
df = _test_data_columbus()

x = df['INC'].values
y = df['HOVAL'].values
Expand Down Expand Up @@ -197,31 +210,33 @@ def test_moran_loc_scatterplot():
scatter_kwds=dict(c='#4393c3'))


def test_lisa_cluster():
columbus = examples.load_example('Columbus')
link_to_data = columbus.get_path('columbus.shp')
df = gpd.read_file(link_to_data)

y = df['HOVAL'].values
w = Queen.from_dataframe(df)
def _test_calc_moran_loc(gdf, var='HOVAL'):
y = gdf[var].values
w = Queen.from_dataframe(gdf)
w.transform = 'r'

moran_loc = Moran_Local(y, w)
return moran_loc


def test_lisa_cluster():
df = _test_data_columbus()
moran_loc = _test_calc_moran_loc(df)

fig, _ = lisa_cluster(moran_loc, df)
plt.close(fig)

# test LineStrings
df_line = _test_LineString()
moran_loc = _test_calc_moran_loc(df_line, var="Length")

def test_plot_local_autocorrelation():
columbus = examples.load_example('Columbus')
link_to_data = columbus.get_path('columbus.shp')
df = gpd.read_file(link_to_data)
fig, _ = lisa_cluster(moran_loc, df_line)
plt.close(fig)

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

moran_loc = Moran_Local(y, w)
def test_plot_local_autocorrelation():
df = _test_data_columbus()
moran_loc = _test_calc_moran_loc(df)

fig, _ = plot_local_autocorrelation(moran_loc, df, 'HOVAL', p=0.05)
plt.close(fig)
Expand All @@ -232,12 +247,13 @@ def test_plot_local_autocorrelation():
aspect_equal=False,
mask=['1', '2', '3'], quadrant=1)
plt.close(fig)

# also test with quadrant and mask
raises(ValueError, plot_local_autocorrelation, moran_loc,
df, 'HOVAL', p=0.05, region_column='POLYID',
mask=['100', '200', '300'], quadrant=1)


def test_moran_loc_bv_scatterplot():
gdf = _test_data()
x = gdf['Suicids'].values
Expand Down

0 comments on commit b30f13e

Please sign in to comment.