Skip to content
This repository has been archived by the owner on Nov 5, 2020. It is now read-only.

Commit

Permalink
fix: correct display of KDE for scatter plot in log scale; ref: give …
Browse files Browse the repository at this point in the history
…up making log-scale contours work with chaco (#231)
  • Loading branch information
paulmueller committed Apr 9, 2019
1 parent 72c9cdc commit 7f3ba9d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 21 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
by chaco testing whether an array object is contained in another
object
- Bugfixes:
- Workaround for plotting contours on a log scale using
https://github.com/enthought/chaco/issues/300
- Scatter plot events are now labeled correctly when log-scale is used
0.9.1
- Add tooltips for box filter settings
- Rename *Statistical analysis* to *Statistical summary* in batch menu
Expand Down
7 changes: 7 additions & 0 deletions shapeout/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,13 @@ def SetParameters(self, newcfg):
pops.append(skey)
for skey in pops:
pl.pop(skey)
# Address issue with faulty contour plot on log scale
# https://github.com/enthought/chaco/issues/300
if (("scale x" in pl and pl["scale x"] == "log") or
("scale y" in pl and pl["scale y"] == "log")):
warnings.warn(
"Disabling contour plot because of chaco issue #300!")
pl["contour plot"] = False
# check for inverted plotting ranges
for feat in dfn.scalar_feature_names:
fmin = feat + " min"
Expand Down
53 changes: 37 additions & 16 deletions shapeout/gui/plot_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ def set_contour_data(plot, analysis):
xax = mm.config["plotting"]["axis x"].lower()
yax = mm.config["plotting"]["axis y"].lower()

scalex = mm.config["plotting"]["scale x"].lower()
scaley = mm.config["plotting"]["scale y"].lower()

plot.index_scale = scalex
plot.value_scale = scaley

if mm.config["filtering"]["enable filters"]:
x0 = mm[xax][mm._filter]
y0 = mm[yax][mm._filter]
Expand All @@ -131,18 +137,29 @@ def set_contour_data(plot, analysis):
break

kde_type = mm.config["plotting"]["kde"].lower()
kde_kwargs = plot_common.get_kde_kwargs(x=x0, y=y0, kde_type=kde_type,
xacc=mm.config["plotting"]["kde accuracy "+xax],
yacc=mm.config["plotting"]["kde accuracy "+yax])
kde_kwargs = plot_common.get_kde_kwargs(
x=x0,
y=y0,
kde_type=kde_type,
xacc=mm.config["plotting"]["kde accuracy "+xax],
yacc=mm.config["plotting"]["kde accuracy "+yax])
# Accuracy for plotting contour data
xacc = mm.config["plotting"]["contour accuracy "+xax]
yacc = mm.config["plotting"]["contour accuracy "+yax]

a = time.time()
(X,Y,density) = mm.get_kde_contour(xax=xax, yax=yax, xacc=xacc, yacc=yacc,
kde_type=kde_type, kde_kwargs=kde_kwargs)
X, Y, density = mm.get_kde_contour(xax=xax,
yax=yax,
xacc=xacc,
yacc=yacc,
xscale=scalex,
yscale=scaley,
kde_type=kde_type,
kde_kwargs=kde_kwargs,
)

print("...KDE contour time {}: {:.2f}s".format(kde_type, time.time()-a))

pd.set_data(cname, density)

# contour widths
Expand All @@ -157,10 +174,15 @@ def set_contour_data(plot, analysis):
if mode == "fraction":
plev = list(np.nanmax(density) * levels)
elif mode == "quantile":
pdensity = mm.get_kde_scatter(xax=xax, yax=yax,
pdensity = mm.get_kde_scatter(xax=xax,
yax=yax,
xscale=scalex,
yscale=scaley,
kde_type=kde_type,
kde_kwargs=kde_kwargs)
kde_kwargs=kde_kwargs,
)
plev = list(np.nanpercentile(pdensity, q=levels*100))

else:
raise ValueError("Unknown contour level mode `{}`!".format(mode))

Expand All @@ -171,23 +193,22 @@ def set_contour_data(plot, analysis):
styles = "solid"
widths = cwidth

scalex = mm.config["plotting"]["scale x"].lower()
scaley = mm.config["plotting"]["scale y"].lower()
plot.index_scale = scalex
plot.value_scale = scaley

cplot = plot.contour_plot(cname,
name=cname,
type="line",
xbounds=(X[0][0], X[0][-1]),
ybounds=(Y[0][0], Y[-1][0]),
xbounds=X,
ybounds=Y,
levels=plev,
colors=mm.config["plotting"]["contour color"],
styles=styles,
widths=widths,
index_scale=scalex,
value_scale=scaley,
)[0]
# Workaround for plotting contour data on a log scale
# (https://github.com/enthought/chaco/issues/300)
# 2019-04-09: This does not resolve the problem. In case of a
# logarithmic scale, there is an offset in the contour plotted.
if scalex == "log":
cplot.index_mapper._xmapper = ca.LogMapper(
range=cplot.index_range.x_range,
Expand All @@ -196,5 +217,5 @@ def set_contour_data(plot, analysis):
if scaley == "log":
cplot.index_mapper._ymapper = ca.LogMapper(
range=cplot.index_range.y_range,
screen_bounds=cplot.index_mapper.screen_bounds[2:]
screen_bounds=cplot.index_mapper.screen_bounds[:2]
)
16 changes: 13 additions & 3 deletions shapeout/gui/plot_scatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,23 @@ def set_scatter_data(plot, mm):


kde_type = mm.config["plotting"]["kde"].lower()
kde_kwargs = plot_common.get_kde_kwargs(x=x, y=y, kde_type=kde_type,

kde_kwargs = plot_common.get_kde_kwargs(x=x,y=y, kde_type=kde_type,
xacc=mm.config["plotting"]["kde accuracy "+xax],
yacc=mm.config["plotting"]["kde accuracy "+yax])

a = time.time()
density = mm.get_kde_scatter(xax=xax, yax=yax, positions=positions,
kde_type=kde_type, kde_kwargs=kde_kwargs)
scalex = mm.config["plotting"]["scale x"].lower()
scaley = mm.config["plotting"]["scale y"].lower()

density = mm.get_kde_scatter(xax=xax,
yax=yax,
positions=positions,
kde_type=kde_type,
xscale=scalex,
yscale=scaley,
kde_kwargs=kde_kwargs,
)
print("...KDE scatter time {}: {:.2f}s".format(kde_type, time.time()-a))

pd = plot.data
Expand Down

0 comments on commit 7f3ba9d

Please sign in to comment.