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

Commit

Permalink
fix: automatically reset kde/contour accuracy when scale (linear/log)…
Browse files Browse the repository at this point in the history
… of an axis changes (finally close #231)
  • Loading branch information
paulmueller committed Apr 11, 2019
1 parent 96b31ea commit 7de3bc0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 16 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
0.9.2
- Allow to set contour levels at quantiles instead of KDE fractions (#180)
- Allow to manually select contour levels
- Log-scale contour plots are now available (#231)
- Pin numpy version to <=1.13.0 to prevent ValueError caused
by chaco testing whether an array object is contained in another
object
- Log-scale contour plots are now available in expert mode
(be careful when setting the contour accuracies)
- Bugfixes:
- Log-scale scatter plots did not show log-scale KDE data (#231)
- Contour lines were shifted by a small amount due to an uncommon
Expand Down
58 changes: 45 additions & 13 deletions shapeout/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,14 @@ def _complete_config(self, measurements=None):
["kde accuracy {}", self._doanes_formula_acc, 1/2],
]
pltng = mm.config["plotting"]
for kk in self.GetPlotAxes():
for kk, sc in zip(self.GetPlotAxes(), self.GetPlotScales()):
for d, l, mult in defs:
var = d.format(kk)
if var not in pltng:
acc = l(mm[kk]) * mult
data = mm[kk]
if sc == "log":
data = np.log(data)
acc = l(data) * mult
# round to make it look pretty in the GUI
accr = float("{:.1e}".format(acc))
pltng[var] = accr
Expand All @@ -155,7 +158,7 @@ def _doanes_formula_acc(a):
"""Compute accuracy (bin width) based on Doane's formula"""
# https://en.wikipedia.org/wiki/Histogram#Number_of_bins_and_width
# https://stats.stackexchange.com/questions/55134/doanes-formula-for-histogram-binning
bad = np.isnan(a) + np.isinf(a)
bad = np.isnan(a) | np.isinf(a)
data = a[~bad]
n = data.size
g1 = scipy.stats.skew(data)
Expand Down Expand Up @@ -371,6 +374,10 @@ def GetPlotGeometry(self, mid=0):
return (int(p["Rows"]), int(p["Columns"]),
int(p["Contour Plot"]), int(p["Legend Plot"]))

def GetPlotScales(self, mid=0):
p = self.GetParameters("Plotting", mid)
return [p["scale x"].lower(), p["scale y"].lower()]

def GetStatisticsBasic(self):
"""
Computes Mean, Avg, etc for all data sets and returns two lists:
Expand Down Expand Up @@ -497,7 +504,7 @@ def reset_plot(self):
self.reset_plot_accuracies()
self.reset_plot_ranges()

def reset_plot_accuracies(self):
def reset_plot_accuracies(self, feature_names=None):
""" Set initial (heuristic) accuracies for all plots.
It is not always easy to determine the correct accuracy for
Expand All @@ -518,8 +525,11 @@ def reset_plot_accuracies(self):
self.measurements[0].config["plotting"]["contour fix scale"]):
return

if feature_names is None:
feature_names = dfn.scalar_feature_names

# Remove contour accuracies for the current plots
for key in dfn.scalar_feature_names:
for key in feature_names:
for mm in self.measurements:
for var in ["contour accuracy {}".format(key),
"kde accuracy {}".format(key)]:
Expand Down Expand Up @@ -550,6 +560,21 @@ def PolygonFilterRemove(self, filt):
except ValueError:
pass

def set_config_value(self, section, key, value):
"""Set the section/key value for all measurements
Parameters
----------
section: str
Configuration section, e.g. "imaging", "filtering", or "plotting"
key: str
Configuration key within `section`
value:
Value to set
"""
for mm in self.measurements:
mm.config[section][key] = value

def SetContourColors(self, colors=None):
""" Sets the contour colors.
Expand All @@ -574,6 +599,9 @@ def SetContourColors(self, colors=None):

def SetParameters(self, newcfg):
"""Update the RT-DC dataset configuration"""
scalex, scaley = self.GetPlotScales()
xax, yax = self.GetPlotAxes()

upcfg = {}
if "filtering" in newcfg:
upcfg["filtering"] = newcfg["filtering"].copy()
Expand All @@ -588,14 +616,18 @@ def SetParameters(self, newcfg):
pops.append(skey)
for skey in pops:
pl.pop(skey)
# Only plot log-space contours in expert mode
config = SettingsFile()
if ((("scale x" in pl and pl["scale x"] == "log")
or ("scale y" in pl and pl["scale y"] == "log"))
and not config.get_bool("expert mode")):
warnings.warn(
"Disabling contour plots on log-scale (no expert mode)!")
pl["contour plot"] = False

# If the scale changed, recompute kde and contour accuracies.
if "scale x" in pl and pl["scale x"] != scalex:
self.set_config_value("plotting", "scale x", pl["scale x"])
self.reset_plot_accuracies(feature_names=[xax])
pl.pop("kde accuracy {}".format(xax))
pl.pop("contour accuracy {}".format(xax))
if "scale y" in pl and pl["scale y"] != scaley:
self.set_config_value("plotting", "scale y", pl["scale y"])
self.reset_plot_accuracies(feature_names=[yax])
pl.pop("kde accuracy {}".format(yax))
pl.pop("contour accuracy {}".format(yax))
# check for inverted plotting ranges
for feat in dfn.scalar_feature_names:
fmin = feat + " min"
Expand Down
2 changes: 1 addition & 1 deletion shapeout/gui/controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,12 @@ def OnChangePlot(self, e=None, updp=True):
mm.config["plotting"]["contour color"] = col.tolist()

cfg = {"plotting": newfilt }

self.analysis.SetParameters(cfg)

# Update Plots
self.frame.PlotArea.Plot(self.analysis)


if updp:
self.UpdatePages()
wx.EndBusyCursor()
Expand Down
3 changes: 3 additions & 0 deletions shapeout/gui/plot_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ def set_contour_data(plot, analysis):
kde_kwargs=kde_kwargs,
)

if X.shape[0] == 1 or X.shape[1] == 1:
raise ValueError("Please decrease value for contour accuracy!")

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

# contour widths
Expand Down

0 comments on commit 7de3bc0

Please sign in to comment.