diff --git a/DataProcessingTools/objects.py b/DataProcessingTools/objects.py index 19094bd..19cf6b9 100644 --- a/DataProcessingTools/objects.py +++ b/DataProcessingTools/objects.py @@ -8,6 +8,21 @@ import os +class ExclusiveOptions(): + def __init__(self, options, checked=None): + self.options = options + if checked is None: + self.checked = 0 + else: + self.checked = checked + + def select(self, option): + if option in self.options: + self.checked = self.options.index(option) + + def selected(self): + return self.options[self.checked] + class DPObject(): argsList = [] filename = "" @@ -66,13 +81,19 @@ def update_plotopts(self, plotopts, ax=None, splotopts=None): ax = plt.gca() replot = False for (k, v) in plotopts.items(): - if isinstance(v, dict): + if isinstance(splotopts[k], ExclusiveOptions): + for (kk, vv) in v.items(): + if vv: + splotopts[k].select(kk) + replot = True + break + elif isinstance(v, dict): self.update_plotopts(v, ax, self.plotopts[k]) else: if v != splotopts[k]: splotopts[k] = v if k == "indexer": - self.upate_index(v) + self.update_index(v) replot = True if replot: diff --git a/tests/test_objects.py b/tests/test_objects.py index 9a67c87..fc3e1c6 100644 --- a/tests/test_objects.py +++ b/tests/test_objects.py @@ -14,16 +14,18 @@ def __init__(self, *args, **kwargs): DPT.objects.DPObject.__init__(self, *args, **kwargs) for i in range(len(self.dirs)): self.setidx.extend([i for j in range(3)]) - self.plotopts = {"exponent": 1.0} + self.plotopts = {"exponent": 1.0, + "color": DPT.objects.ExclusiveOptions(["blue","red"], 0)} def plot(self, i=None, ax=None): if ax is None: ax = plt.gca() if not self.plotopts.get("overlay", False): ax.clear() + color = self.plotopts["color"].selected() x = np.array([0, 1, 2, 3]) y = x**self.plotopts["exponent"] - ax.plot(x, y) + ax.plot(x, y, color=color) def test_plot(): cwd = "Pancake/20130923/session01/array01/channel001/cell01" @@ -43,6 +45,8 @@ def test_plot(): obj.update_plotopts({"exponent": 2.0}, ax=ax) xy = ax.lines[0].get_data() assert np.allclose(xy[1], x**2.0) + obj.update_plotopts({"color": {"red": True}}, ax=ax) + assert ax.lines[0].get_color() == "red" def test_append(): tempdir = tempfile.gettempdir()